Current unix time / stamp :


Unix time in PHP

PHP may be the scripting language with the most and the strongest Unix time functions, and also the easiest to work with. This page will show you these functions and describe how to use them to get on top of Unix time, and times and dates by extension.

Getting the current Unix timestamp

To obtain the current Unix time in PHP, simply use the time() function. You don't even have to pass any parameters!

$unixtime = time();

Converting a date and/or time into a Unix timestamp

Again, PHP does all the magic for you, simply use the strtotime() function and pass the textual datetime string you want to convert as its parameter. PHP will handle the rest and return a Unix timestamp, or -1 or false if it fails to understand your input. Note that PHP5 is much smarter about what it can understand date-wise than everything prior, so I recommend that you enable it explicitly.

$date_to_unixtime = strtotime('25th October 2009 11:12:34 PM (UTC)');

Did you know? strtotime() can also accept relative dates, like for example last thursday, +1 year 2 months 3 weeks 4 days seconds and so on. Experiment!

Converting a Unix timestamp into a date-time string

When you need to convert a Unix timestamp into a normal date with PHP, the date() function can handle this in a jiffy, with whatever format you could desire. You can use the converter to experiment with the PHP time format symbols until you get the format you want, just open the time format options slide and it's all there.

$unixtime_to_date = date('jS F Y h:i:s A (T)', $unixtime);

If you don't include the second parameter to date(), PHP will assume the current time.