PHP 将日期时间转换为秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4392040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP convert datetime to seconds
提问by dzm
I have a datetime value in mysql
'2010-12-08 16:12:12'
that I'd like to get the seconds to that date using PHP
,
so basically a PHP
equivalent of mysql
:
我在mysql
'2010-12-08 16:12:12'
中有一个日期时间值,我想使用 获得该日期的秒数PHP
,
所以基本上PHP
相当于mysql
:
TIME_TO_SEC(TIMEDIFF('2010-12-08 16:12:12',now()))
回答by ajreal
huh ? these function are from mysql
...
嗯?这些功能来自mysql
...
For PHP, you replace it using strtotime
对于 PHP,您可以使用替换它 strtotime
$diff = strtotime('2010-12-08 16:12:12')-time();
回答by davidtbernal
<?php
$date1 = new DateTime("2010-12-08 16:12:12");
$now = new DateTime();
$difference_in_seconds = $date1->format('U') - $now->format('U');
->format('U')
turns it into a unix timestamp.
->format('U')
把它变成一个unix时间戳。
回答by Anush Prem
try
尝试
$time_diff = time() - strtotime('2010-12-08 16:12:12');