首页 > 解决方案 > PHP时间不同

问题描述

问题:尽管数据库中的输出是正确的(时间戳),但输出是错误的

DB截图:http: //prntscr.com/mjftzn

代码是输出

    $dbDate = strtotime(date('Y-m-d H:i:s')); // Database date
    $endDate =  strtotime("".$query['duedate']."");    // current time
    $diff = $endDate - $dbDate; /// diffrence
     $days = intval(intval($diff) / (3600*24));
$newDays = $days < 0 ? 'EXPIRED' : $days;

代码结果:http: //prntscr.com/mjfw4t

预期:剩余 14 天而不是过期,如果 -1 将显示过期

标签: php

解决方案


这是计算两个日期之间差异的方法:

$date1 = "2007-03-24";
$date2 = "2009-06-26";

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));


printf("%d years, %d months, %d days\n", $years, $months, $days);

现在用你自己的逻辑来实现它。


推荐阅读