doudao1922 2019-05-05 10:12
浏览 15

如何在PHP中比较2次

I want to check current times is greater than 12.00 pm or not in PHP I have tried this code

<?php
       date_default_timezone_set('Asia/Kolkata');
       $currentTime = date( 'h:i:s A', time () );
       echo $currentTime;
              //var_dump($currentTime);


       $tm="12:00:00 PM";
      $from= date("h:i:s A", strtotime($tm));
      echo $from;
      //var_dump($from);
      if($currentTime>$from){
      echo "success";
       }
 ?>

but it is comparing times as string. If the current time is 2.00pm it is not printing success.

  • 写回答

1条回答 默认 最新

  • douzai1074 2019-05-05 14:51
    关注

    Use the binary timestamps for calculations and comparing, and convert them only to text for displaying. This is more efficient than managing all as time strings. Add differences can be build by a simple and fast subtraction ($delta = $currentTime - $from).

    The modified code may look like this:

    <?php
    date_default_timezone_set('Asia/Kolkata');
    
    $currentTime = time(); // very fast function
    echo date('h:i:s A',$currentTime);
    
    $tm="12:00:00 PM";
    $from = strtotime($tm);
    echo date('h:i:s A',$from);
    
    if ( $currentTime > $from ){
       $delta = currentTime - $from;
       echo "success, delta $delta seconds";
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver