views:

460

answers:

3

I am looking to convert a mysql timestamp to a epoch time in seconds using php, and vice versa. What's the cleanest way to do this?

+5  A: 

There are two functions in MySQL which are useful for converting back and forth from the unix epoch time that PHP likes:

from_unixtime()

unix_timestamp()

For example, to get it back in PHP unix time, you could do:

SELECT unix_timestamp(timestamp_col) FROM tbl WHERE ...
Harrison Fisk
+6  A: 

See strtotime and date functions in PHP manual.

$unixTimestamp = strtotime($mysqlDate);
$mysqlDate = date('Y-m-d h:i:s', $unixTimestamp);
Michał Rudnicki
A: 

From MySQL timestamp to epoch seconds:

strtotime($mysql_timestamp);

From epoch seconds to MySQL timestamp:

$mysql_timestamp = date('Y-m-d H:i:s', time());
Magsol