Time
2003-07-03 14:20:16
Category: php:math
Description: Some examples on creating and comparing timestamps with php.
Author: detour
Viewed: 8639
Rating: (28 votes)


<?php
/* time.php by detour@metalshell.com
 *
 * Some examples on creating and comparing timestamps
 * with php.
 *
 * http://www.metalshell.com/
 *
 */
 
 
// Retrieve the number of seconds since the Unix Epoch (January 1, 1970)
$curtime = time();
 
/* Format the date to be human readable.  The date function converts
   specific characters into readable words and numbers. */
$curdate = date("F j, Y h:i:s A", $curtime);
 
print "The current date is $curdate<br>";
 
/* Often dates are stored in readable format however they have to be
   converted back into a timestamp to compare times.  PHP provides the
   strtotime() function that will convert most any readable date format
   into a Unix timestamp. */
print "$curtime should equal " . strtotime($curdate) . "<br>";
 
// Sleep five seconds to compare two times.
print "Sleeping for five seconds.<br>";
sleep(5);
 
print "This program has been running for " . (time() - $curtime) . 
      " seconds.<br>";
 
/* You can also create your own timestamp from a time using mktime(). 
   The format is int mktime(hr, min, sec, month, day, year, is_dst)
   is_dst is used define whether or not to use daylight savings. 
   Any of these values may be ommitted. */
 
print "The timestamp for 12am on 11/26/81 is " . mktime(0,0,0,11,26,81) . 
      "<br>";
 
/* If you are bench marking a program microtime is probably what you 
   will want to use.  It returns microseconds and a normal timestamp divided 
   by a space. */
list($msec, $sec) = split(" ", microtime());
$fltime = (float)$msec + (float)$sec;
print "This program ran exactly " . ($fltime - $curtime) .
      " seconds<br>";
 
?>