dongzhuo0895 2017-12-15 07:52
浏览 13
已采纳

检查数据库中字段的剩余时间

I've got this filed in database stored as type DATETIME.
Consider the value for it: 2017-12-14 16:17:21

And this is my program logic. I want to check if 72 hours passed else echo you need to wait 48 hours and 22 minutes and 55seconds for example.

$row = $res->fetch_assoc();
$last_dl = $row['last_dl'];

// To seconds from 1970
$last_dl = strtotime($last_dl); //120
$seventy_two_hours_in_seconds = 60 * 60 * 72;

if (time() > ($last_dl + $seventy_two_hours_in_seconds)) {
    echo "72 Passed";
} else {
    echo "You need to wait: " . date('H/h i/m s/s',mktime(0, 0, ($last_dl + $seventy_two_hours_in_seconds) - time()));
}

What is wrong with the logic. Cause it shows way less hours.

  • 写回答

1条回答 默认 最新

  • duanba3707 2017-12-15 08:39
    关注

    You can calculate time in second to wait, and after present it in hours, minutes and seconds:

    <?php
    $row = $res->fetch_assoc();
    $last_dl = $row['last_dl'];
    
    // To seconds from 1970
    $last_dl = strtotime($last_dl); //120
    $seventy_two_hours_in_seconds = 60 * 60 * 72;
    
    if (time() > ($last_dl + $seventy_two_hours_in_seconds)) {
        echo "72 Passed";
    } else {
        $wait = date('U', $last_dl + $seventy_two_hours_in_seconds) - date('U'); // seconds for wait
        $hours = floor($wait /3600);
        $min = floor(($wait / 60) % 60);
        $sec = $wait % 60;
        echo "You need to wait: ". $hours. ':'. $min.':'.$sec ."
    ";
    }
    

    http://sandbox.onlinephpfunctions.com/code/08477cd78c82dfe2b4a5c5f4aee6414866999a8c

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊