duanni5726 2012-07-26 12:46
浏览 9
已采纳

条件时间差仅在行尚未开始时显示特定消息

These are the inputs an database rows

$current              2012-07-26 15:30:00
1st $row['start']     2012-07-26 14:00:00
2nd $row['start']     2012-07-26 17:00:00

When I run the following code with the above current time, I get correctly the "Starts soon" for the 2nd row, but also I get it mistakenly on the 2nd row that it already started.

How do I edit this code to return me the "Starts soon" message only to the rows that will start in the next two hours?

$diff = strtotime($row['start']) - strtotime($current);
if ($diff < 7200) {
    echo 'Starts soon';
} else if ($diff <= 0) {
    echo 'Started';
} else {
    echo 'Starts';
}
  • 写回答

1条回答 默认 最新

  • doujia1939 2012-07-26 12:49
    关注

    All $diff matching your second if clause (< 0) are already caught by the first if clause (< 7200) and never reach the second if in that else clause.

    As a solution, restructure your code in the following way:

    $diff = strtotime($row['start']) - strtotime($current);
    if ($diff <= 0) {
        echo 'Started';
    } else if ($diff < 7200) {
        echo 'Starts soon';
    } else {
        echo 'Starts';
    }
    

    EDIT

    With respect to the question in comments:

    If you mean the calendar day, you could use the following code:

    if ( date( 'zY', $current ) == date( 'zY', $row['start'] ) {
      // same day
    } else {
      // different days
    }
    

    If you mean just that both times are no more than 24 hours apart, use

    if ( abs($current - $row['start']) < 24 * 60 * 60 ) {
      // same day
    } else {
      // different day
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题