如果当前日期介于PHP范围之间,这种方式是否可靠

时间:2017-03-06 11:53:38

标签: php date strtotime

请告诉我以下方法是否可靠,以检查当前日期是否在范围之间

function check_in_range($start_date, $end_date)
 {
          // Convert to timestamp
          $start_ts = strtotime($start_date);
          $end_ts = strtotime($end_date)+86400;
          //added 86400 to check for that particular date too

          $timeNow = strtotime("now");

          // Check that user date is between start & end
          return (($timeNow >= $start_ts) && ($timeNow <= $end_ts));
  }

1 个答案:

答案 0 :(得分:0)

我建议使用日期时间对象,如下所示:

function check_in_range($start_date, $end_date)
    {
        $startDate = new \DateTime($start_date);

        $endDate = new \DateTime($end_date);
        $endDate->add(new \DateInterval('PT86400'));

        return ((time() >= $startDate->getTimeStamp()) && (time() <= $endDate->getTimestamp()));
    }