douhunbei0166 2016-03-30 05:56
浏览 99
已采纳

可以像这样比较date()吗? [重复]

This question already has an answer here:

I have had some issues with comparing date/times.

I had a working code that just stopped working all of a sudden. Now I made some changes to it and by some reason UNIX time is not correct.

Utime and nexttime is just an example string now, but in the code Utime is last time updated, and nexttime is next time a update will happen calculated from Utime.

Example:

 $Utime = "201603300450"; // YmdHi 2016 03 30 04:50
 $nexttime = "201603300520"; 
 if (date("YmdHi")>=$Utime && date("YmdHi")>=$nexttime)

Is this a safe way to compare the timevalue? I had some issues with strtotime thats why I ask.

</div>
  • 写回答

3条回答 默认 最新

  • douzai6337 2016-03-30 06:08
    关注

    You should convert your dates to DateTime objects:

    <?php
    
    $Utime = "201603300450";
    $nexttime = "201603300520";
    
    $dateUtime = DateTime::createFromFormat('YmdHi', $Utime);
    $datenexttime = DateTime::createFromFormat('YmdHi', $nexttime);
    
    $nowDate = DateTime::createFromFormat('YmdHi', date('YmdHi'));
    
    if ($nowDate>=$dateUtime && $nowDate>=$datenexttime)
        //do your stuff...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗