I need to get the number of seconds since 1970 untill the time I got after calling localtime
. Is there a simple way to convert what is returning from localtime (1393340639)
for example back to seconds?
Do not use localtime
. Since PHP 5.2 was release, back last November 2nd 2006, you can use DateTime
which offers a better interface to play with time, and which is compatible with UNIX timestamps (what you're looking for).
For example :
// always work with UTC!
date_default_timezone_set('UTC');
// support your legacy need...
$lt = localtime(1393340639);
$dt = new DateTime();
// Note : DateTime's year is 0 based, while localtime's 1900 based, so we add 1900
// Note : DateTime's month is 1 based, while localtime's 0 based, so we add 1
$dt->setDate($lt[5] + 1900, $lt[4] + 1, $lt[3]);
$dt->setTime($lt[2], $lt[1], $lt[0]);
//$dt->setTimestamp(1393340639); // <-- SAME THING!
var_dump($dt);
// output:
// object(DateTime)#1 (3) {
// ["date"]=>
// string(19) "2014-02-25 15:03:59"
// ["timezone_type"]=>
// int(3)
// ["timezone"]=>
// string(3) "UTC"
// }
echo $dt->getTimestamp();
// output: 1393340639 (which is your initial timestamp value)
You're better off not using localtime
altogether, though. Just pass a DateTime
object around.
If you're looking for number of seconds from 1970 till a function being called, you can use <?PHP echo time() ?>