如何检查时间是否大于或小于

时间:2017-10-20 23:16:22

标签: php

我正在处理我的PHP脚本以从网站上删除数据。在第1天到第7天,如果该标志为0,我想查看时间,以查看时间是否小于12:00 AM,那么我想使用flag = 1

示例:时间10:00 PM11:59 PM小于12:00 AM,因此我可以使用flag = 1

以下是代码:

if($day>=0 && $day <= 7)
{
   $flag=0;

   if($day >= 1)
   {
      if ($flag == 0)
      {
         print_r($show_times);
      }
   }
}

结果如下:

Array ( [0] => 22:00 [1] => 03:00 [2] => 04:00 [3] => 06:00 [4] => 08:00 [5] => 10:00 [6] 
=> 11:00 [7] => 12:00 [8] => 13:00 [9] => 13:55 [10] => 14:25 [11] => 14:55 [12] 
=> 16:55 [13] => 18:10 [14] => 18:40 [15] => 19:00 [16] => 19:30 [17] => 20:00 [18] => 20:55 [19] 
=> 21:55 [20] => 23:10 [21] => 00:40 [22] => 01:00 [23] => 01:30 [24] => 02:00 ) 10:00 PM KSW 39

以下是完整代码:

<?php
function get_shows($day,$channel_id, DateTime $dt, $today = false) 
{

   $ch = curl_init();
   curl_setopt_array($ch, array(
      CURLOPT_USERAGENT => '',
      CURLOPT_TIMEOUT => 30,
      CURLOPT_CONNECTTIMEOUT => 30,
      CURLOPT_HEADER => false,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_MAXREDIRS => 5,
      CURLOPT_SSL_VERIFYPEER => false
   ));

   $date = $dt->format('Y-m-d');
   $tz = $dt->getTimezone();

   $now = new DateTime('now', $tz);
   $today = $now->format('Y-m-d');
   $shows = array();
   $url = 'https://www.example.com/scrip.php?date=' . $date;
   curl_setopt($ch, CURLOPT_URL, $url);
   $body = curl_exec($ch); //get the page contents

   $channel_row = $row_channels[0][0]; // Woksepp: 0 = First row.

    $pattern23 = "/<a class=\"prog\" href=\"(.*?)\">.*?<span class=\"time\">(.*?)<\/span>.*?<span class=\"title\" href=\"\#\">(.*?)<\/span>.*?<span class=\"desc\">(.*?)<\/span>/s";
    preg_match_all($pattern23, $channel_row, $d);
    $show_times = $d[2];

    //test this
    if($day>=0 && $day <= 7)
    {
        $flag=0;

        if($day >= 1)
        {
           if ($flag == 0)
           {
              print_r($show_times);
           }
        }
    }
}

我期待做的是我想检查时间显示22:00或中午之前00:00之前是什么,然后我想将标志设置为1所以我可以跳过阵列中的时间。

您能否请我举例说明如何检查22:0023:59之间的时间,看看时间是否小于00:00,以便我可以使用flag = 1在if声明下?

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

if($day >= 1)
{
    foreach($show_times as $show_time)
    {
        if(strtotime($show_time) < strtotime('12:00'))
         {
             $flag=1;
         }
    }
}

这将检查时间是否小于noon并将$flag设置为1