doumicheng6732 2016-06-21 12:30
浏览 40

PHP中的多个运算符如果语句

Just wanted to double check and make sure my line of thinking is correct. This hook runs every 48 hours, so I need to check if an event is happening today or tomorrow.

 $now = date('Y/m/d');
$today = explode("/", $now);

The event start date has the same format, but the value varies, and is stored the same way.

if ( $today[1] == $eventDate[1] &&
(intval($today[2]) == (intval($eventDate[2]-1)) || (intval($today[2]) == intval($eventDate[2])-2))) {

//run code

}
  • 写回答

1条回答 默认 最新

  • dounianji7883 2016-06-21 12:37
    关注

    In my opinion, you should consider to work with DateTime objects :

    $today = new \DateTime();
    $eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
    
    if ($today->format('d-m-Y') === $eventDateTime->format('d-m-Y')) {
        ...
    }
    

    If you want to check that an event is happening today or tomorrow, you could do like this :

    $start = new \DateTime();
    $start->setTime(0,0,0);
    $end = new \DateTime();
    $end->add(new \DateInterval('P1D'));
    $end->setTime(23,59,59);
    $eventDateTime = \DateTime::createFromFormat('Y/m/d', $eventDate);
    
    if ($eventDateTime >= $start && $eventDateTime <= $end) {
        ...
    }
    

    This way you check if a date is in a certain period. Here I added 1 day to the current date but you can adapt the code and set more than 1 day if you want.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探