Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从MySQL日期时间检查PHP中经过的时间_Php_Mysql - Fatal编程技术网

从MySQL日期时间检查PHP中经过的时间

从MySQL日期时间检查PHP中经过的时间,php,mysql,Php,Mysql,我如何在MySQL数据库中检查从当前时间到当前时间的时间间隔 示例:当前时间是2018-07-21 10:04:20,我的数据库中的时间是2018-07-20 21:58:40 我想知道从数据库中的日期时间到当前日期时间的秒数。只需使用strotime将日期转换为数字即可 $seconds = abs(strtotime($currentDate) - strtotime($dbDate)); 如果结果为负数,我将abs strotime返回01-01-1970和参数中日期之间的秒差。您可以使

我如何在MySQL数据库中检查从当前时间到当前时间的时间间隔

示例:当前时间是2018-07-21 10:04:20,我的数据库中的时间是2018-07-20 21:58:40


我想知道从数据库中的日期时间到当前日期时间的秒数。

只需使用
strotime
将日期转换为数字即可

$seconds = abs(strtotime($currentDate) - strtotime($dbDate));
如果结果为负数,我将
abs


strotime
返回
01-01-1970
和参数中日期之间的秒差。

您可以使用strotime()将日期时间转换为Unixtime和时间()以获取实际时间戳:

    $timestamp = strtotime("2018-07-21 10:04:20"); //replace with your DB-date
    $diff =  time() - $timestamp ;                 //calculate the Difference
    echo $diff;                                    //negative number is in the future

strotime
我想到了数学。。。或者在DB端@user3783243,是的,我可以使用strotime,但如果我将strotime(curr_time)-strotime(DB_time)=n=我不知道如何将n转换为秒,这将以秒为单位,因为这两个值都是自1970年以来的秒数。如果为负,则可能会转到
0
。使用
abs
,您可能会大大颠倒结果。不,我的意思是,在这种情况下,数据库中的日期可能大于当前日期。我数据库中的所有日期时间要么等于当前时间,要么晚于当前时间。它不能领先于当前时间