php - 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 将显示过期
解决方案
这是计算两个日期之间差异的方法:
$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);
现在用你自己的逻辑来实现它。
推荐阅读
- python - Python Camelot-py解析中缺少值
- regex - 字符类中的正则表达式重复范围
- html - Visual Studio Code:在 CSS 中选择类后在浏览器中突出显示对象边框
- mysql - 成功完成 MySQL 触发器后如何返回消息
- c++ - CMake 找不到文件或目录
- javascript - React Native 在生产模式下仅显示启动画面
- c# - 基于需要在执行时间内接收更多项目的列表执行 Parallel.ForEach
- javascript - 第二次通过后阵列未重置
- typescript - Jest mockImplementation 不是一个函数,但可以与 require 一起使用
- bash - 如何在 Bash 脚本中使用 JQ 通过变量修改 json 文件?