Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Working with Date and Time Wish List | Report Bug | About Me ]

4.5.1     Reading the current time: time() and microtime()

This is NOT the latest copy of this book; click here for the latest version.

int time ( )

mixed microtime ( [bool get_as_float])

PHP has a basic function to get the current time in epoch format: time(). Time() takes no parameters, and returns the current timestamp representing the current time. As time() is the first function we have looked at, here is an example script:

<?php
    
print time();
    
$foo = time();
    print
$foo;
?>

As you can see, we can either print the return value of time() directly, or we can store it away in a variable then print the contents of the variable - the result is identical.

Working in Unix time means you are not tied down to any specific formatting - you do not need to worry about whether your date has months before days or vice versa, whether long months are used, whether day numbers of day words (Saturday, Tuesday, etc) are used, and so on.

Furthermore, to add one to a day (that is, to get the date of tomorrow), you can just add one day's worth of seconds to your current timestamp: 60 x 60 x 24 = 86400. So, adding or subtracting 86400 to a date moves forward by one day, and so on - easy, really.

For more precise time values, you can use the microtime() function. When called without any parameters, this returns the current system time in seconds and microseconds, ordered microseconds first. For example: 0.82112000 1174676574

If you pass true to microtime() as its only parameter, PHP will return the time in a more obvious format - seconds.microseconds, like this: 1174676587.5996

When using microtime(), keep in mind that the return value is a floating-point number. There is a setting in your php.ini file called "precision", which sets the number of significant digits to show in floating-point numbers - note that is significant digits, not decimal places, which means your return value from microtime() may not be as precise as you want. Above, for example, you can see we only have four decimal places returned - this is because php.ini defaults precision to 14, and there are ten digits before the decimal place.

If you increase the value of precision up to, say, 18, and run microtime() again, you will get results that are more accurate: 1174677004.8997819.





<< 4.5 Working with Date and Time   4.5.2 Converting from a string: strtotime() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
un_xs@yahoo.com - 29 Aug 2008

echo date("g:i A");

g - hours
i - minutes
A - AM or PM

this is how to get he time

A certifiable nutcase - 29 Aug 2008

The exact time when I tested this script was 1140222222...

Six 2's in a row!! Coincidence you say? No, say I; it's a sign and it means:

Turn off the computer, see a shrink and get a life!!

Unix Programmer - 29 Aug 2008

I find the comment about daylight savings time interesting. The function being discussed is time(), which returns a UNIX timestamp (aka UNIX epoch). The UNIX timestamp is the number of seconds since midnight, Jan 1, 1970 GMT. Because this time is relative to Greenwich Mean Time, daylight savings time is not relevant.

Daylight savings time comes into play in converting the UNIX timestamp into a readable string. That is covered in a later chapter.

So, adding or subtracting 86400 seconds to a timestamp value is always a valid way to get to a later or earlier day.

Roberto Ibarra - 29 Aug 2008

Passing True to microtime() only works in PHP5.

A PHP User - 29 Aug 2008

for a better way to get tomorrow's time use strtotime("tomorrow");
strtotime("next week");

this function takes daylight savings into account

A PHP User - 29 Aug 2008

Note: In the U.S., because of daylight savings time, adding or subtracting 86400 seconds is NOT guaranteed to get you the next or previous day.

A PHP User - 29 Aug 2008

pretty straight-forward for a confusing subject.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow