How do I convert between time formats?

2019-06-07 04:56发布

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?

3条回答
做个烂人
2楼-- · 2019-06-07 05:28

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());
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-06-07 05:40

See strtotime and date functions in PHP manual.

$unixTimestamp = strtotime($mysqlDate);
$mysqlDate = date('Y-m-d h:i:s', $unixTimestamp);
查看更多
孤傲高冷的网名
4楼-- · 2019-06-07 05:43

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 ...
查看更多
登录 后发表回答