PHP function to check time between the given range?

后端 未结 2 1496
孤城傲影
孤城傲影 2020-12-10 22:30

I am using PHP\'s Date functions in my project and want to check weather a given date-time lies between the given range of date-time.i.e for example if the current date-time

2条回答
  •  醉梦人生
    2020-12-10 22:45

    The function to check if date/time is within the range:

    function check_date_is_within_range($start_date, $end_date, $todays_date)
    {
    
      $start_timestamp = strtotime($start_date);
      $end_timestamp = strtotime($end_date);
      $today_timestamp = strtotime($todays_date);
    
      return (($today_timestamp >= $start_timestamp) && ($today_timestamp <= $end_timestamp));
    
    }
    

    Call function with parameters start date/time, end date/time, today's date/time. Below parameters gets function to check if today's date/time is between 10am on the 26th of June 2012 and noon on that same day.

    if(check_date_is_within_range('2012-06-26 10:00:00', '2012-06-26 12:00:00', date("Y-m-d G:i:s"))){
        echo 'In range';
    } else {
        echo 'Not in range';
    }
    

提交回复
热议问题