Question

How can I convert 2010-03-01 to unixtime such as 1267452738 (using php)?

Was it helpful?

Solution

echo strtotime('2010-03-01');

An alternative is to use DateTime():

// PHP 5.3+
$dt = new DateTime('2010-03-01');
echo $dt->getTimestamp();

// PHP 5.5+
echo (new DateTime('2010-03-01'))->getTimestamp();

OTHER TIPS

From http://php.net/manual/en/function.strtotime.php:

strtotime (PHP 4, PHP 5) — Parse about any English textual datetime description into a Unix timestamp

int strtotime ( string $time [, int $now ] )

The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now , or the current time if now is not supplied.

This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.

http://www.onlineconversion.com/unix_time.htm is also a good resource to convert unix time to date and vice versa.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top