PHP将datetime转换为秒

dzm*_*dzm 11 php mysql datetime

我在mysql'2010-12-08 16:12:12'
中有一个日期时间值,我希望得到该日期的秒数PHP,
所以基本上PHP相当于mysql:

TIME_TO_SEC(TIMEDIFF('2010-12-08 16:12:12',now()))
Run Code Online (Sandbox Code Playgroud)

ajr*_*eal 21

嗯?这些功能来自mysql......

对于PHP,您可以使用它替换它 strtotime

$diff = strtotime('2010-12-08 16:12:12')-time();
Run Code Online (Sandbox Code Playgroud)

详情:http://php.net/manual/en/function.strtotime.php


dav*_*nal 17

<?php

$date1 = new DateTime("2010-12-08 16:12:12");
$now = new DateTime();

$difference_in_seconds = $date1->format('U') - $now->format('U');
Run Code Online (Sandbox Code Playgroud)

->format('U') 将其转换为unix时间戳.

  • $ date-> getTimestamp()可能会更好@notJim http://php.net/manual/en/datetime.gettimestamp.php (3认同)