Home arrow PHP arrow Page 4 - Date/Time Processing with PHP

Race Against Time - PHP

Like most programming languages, PHP comes with a fairlyfull-featured API for date and time value manipulation. You've probablyused it in your applications, but never bothered to look too closely atit. Well, here's your chance to rectify that mistake - this articledelves into the date/time API in depth, uncovering some hidden nuggetsand demonstrating how it can be used to simplify date and timeprocessing in your PHP scripts.

TABLE OF CONTENTS:
  1. Date/Time Processing with PHP
  2. Getting A Date
  3. A Stamp In Time...
  4. Race Against Time
  5. When Looks Do Matter
  6. Checking Up
  7. Turning The Tables
By: The Disenchanted Developer, (c) Melonfire
Rating: starstarstarstarstar / 14
March 19, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
If you don't like mktime(), you can also use the time() function, which returns the current timestamp (note that you cannot use this function to generate arbitrary timestamps, as is possible with mktime()).

If you need greater accuracy in your timestamp, take a look at the microtime() function, which returns a timestamp in seconds, together with an additional microsecond component. These two values can be added together to obtain a more precise timestamp value. Like time(), this function too cannot be used with arbitrary dates and times - it only returns a timestamp for the current instant.

The microtime() function is particularly useful when you need to time events accurately, or when you need an arbitrary, unique number to seed PHP's random number generator. Here's an example of the former:
<?
// function to calculate timestamp in microseconds
function tscalc()
{
// split returned timestamp
$arr = explode(" ", microtime());
// add microseconds to seconds 
// to generate more precise timestamp 
return $arr[0] + $arr[1];
}
// start time
$start = tscalc();
// do something
// over here, run a loop
$count = 0;
$x = 0;
while ($count < 500)
{
$x = $x + $count;
$count++; 
}
// end time
$end = tscalc();
echo "Started at $start";
echo "Ended at $end";
echo "Total processing time was " . ($end-$start);
?>
And here's an example of the latter:
<?
// function to calculate timestamp in microseconds
function tscalc()
{
// split returned timestamp
$arr = explode(" ", microtime());
// add microseconds to seconds 
// to generate more precise timestamp 
return $arr[0] + $arr[1];
}
// seed random number generator
srand(tscalc());
// get a random number
echo rand();
?>


 
 
>>> More PHP Articles          >>> More By The Disenchanted Developer, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: