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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:55:22  来源:igfitidea点击:

PHP convert datetime to seconds

phpmysqldatetime

提问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 PHPequivalent 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();

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

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

回答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');