如何在php中显示特定时间段之间的任何内容?

如何在php中显示特定时间段之间的任何内容?,php,date,if-statement,time,and-operator,Php,Date,If Statement,Time,And Operator,我有一个php代码,如下所示,我想在其中显示一周中两个日历日之间的任何内容 $data->{“选择开始日”}中的值$data->{“开始时间”}$data->{“选择结束日期”}和$data->{“结束时间”}由用户控制 PHP代码: if (file_exists('feeds/ptp-ess_landing.json')) { $data = json_decode(file_get_contents('feeds/ptp-ess_landing.json')); }

我有一个php代码,如下所示,我想在其中显示一周中两个日历日之间的任何内容

$data->{“选择开始日”}中的值
$data->{“开始时间”}
$data->{“选择结束日期”}
$data->{“结束时间”}由用户控制

PHP代码:

    if (file_exists('feeds/ptp-ess_landing.json')) {
    $data = json_decode(file_get_contents('feeds/ptp-ess_landing.json'));
    }

    date_default_timezone_set('America/Toronto');
    $arradate = strtolower(date('D'));
    $nowtime = (int)date('His');


    $start_day=$data->{"select_start_day"};
    $start_time=$data->{"start_time"};

    $end_day=$data->{"select_end_day"};
    $end_time=$data->{"end_time"};
$dowMap = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

$arradate = date('D');

$arradateNum = array_search($arradate, $dowMap);

$start_dayNum = array_search($start_day, $dowMap);

$end_dayNum = array_search($end_day, $dowMap);

if(
    ($arradateNum >= $start_dayNum && $nowtime>=$data->{"start_time"}) && 
    ($arradateNum <= $end_dayNum && $nowtime <= $data->{"end_time"})
) {
    echo "Show Anything";  
}

例如,假设用户输入
$start\u day
作为
sun
$start\u time
as
143400
$end\u day
as
wed
$end\u time
as
140000

上面的持续时间意味着我们在范围内,它应该显示我们想显示的任何东西,直到明天下午2点,因为明天是星期三。我在东部时间

我使用以下代码来提取一周中的当前日期和时间

date_default_timezone_set('America/Toronto');
$arradate = strtolower(date('D'));
$nowtime = (int)date('His');
问题陈述:

我想知道如果我需要使用逻辑,以便它可以打印任何
Sunday
143400
suday
140000

if()    {
    echo "Its in range";
}
案例:

    if (file_exists('feeds/ptp-ess_landing.json')) {
    $data = json_decode(file_get_contents('feeds/ptp-ess_landing.json'));
    }

    date_default_timezone_set('America/Toronto');
    $arradate = strtolower(date('D'));
    $nowtime = (int)date('His');


    $start_day=$data->{"select_start_day"};
    $start_time=$data->{"start_time"};

    $end_day=$data->{"select_end_day"};
    $end_time=$data->{"end_time"};
$dowMap = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

$arradate = date('D');

$arradateNum = array_search($arradate, $dowMap);

$start_dayNum = array_search($start_day, $dowMap);

$end_dayNum = array_search($end_day, $dowMap);

if(
    ($arradateNum >= $start_dayNum && $nowtime>=$data->{"start_time"}) && 
    ($arradateNum <= $end_dayNum && $nowtime <= $data->{"end_time"})
) {
    echo "Show Anything";  
}

  • 如果覆盖应在周一上午8点到周一下午6点应用,而今天是周三,则覆盖不适用

  • 如果是星期一下午6点和星期五下午6点,则覆盖将在星期一、星期二全天、星期三全天、星期四全天、星期五至下午6点工作

  • 如果是星期日下午6点和星期一下午6点,则覆盖将在星期一工作6小时,星期一工作18小时

  • 如果是星期二下午6点和星期五下午6点,而今天是星期二晚上9点,则将应用覆盖

  • 如果是周四下午6点和周三下午6点,则覆盖将在周四的6小时、周五的24小时、周六的24小时、周日的24小时、周一的24小时、周二的24小时工作,周三24小时和周四18小时


  • 我会保持所有日期的格式相同。Unix时间是存储时间的好方法,因为您可以轻松地运行计算

    在您的示例中,您同时使用字符串和数字进行计算。事实上,你有三种不同的方式来表达日期。我建议使用just date()保存和比较所有值,并在向最终用户显示时仅格式化Unix时间戳

    ↓↓↓↓ 新编辑↓↓↓

    仔细查看代码返回的内容:

    $arradate = strtolower(date('D')); //echos the day ex: sun
    $nowtime = (int)date('His'); //echos a different format ex: 25019
    
    并仔细阅读函数的文档,特别是您为nowtime选择的参数,这些参数是“他的”,将返回:

    H一小时的24小时格式,前导零00到23

    i前导零为00到59的分钟数

    s秒,前导零00到59

    现在考虑DATE()返回一个时间戳,你会看到失败。您只需删除所有代码中的日期格式,保存显示给最终用户的元素。知道今天是星期三对计算机没有好处。这台计算机从1970年起才开始计数

    因此,您只需要一个参数来调用当前日期。这两种方法都有效:

    $arradate = date(); 
    $nowtime = date(); 
    

    最后,在阅读了Unix时间之后,您将看到存储的日期是向后的。您的结束时间是第0天,开始时间是第0天之后的两天

    试试这个:

        if (file_exists('feeds/ptp-ess_landing.json')) {
        $data = json_decode(file_get_contents('feeds/ptp-ess_landing.json'));
        }
    
        date_default_timezone_set('America/Toronto');
        $arradate = strtolower(date('D'));
        $nowtime = (int)date('His');
    
    
        $start_day=$data->{"select_start_day"};
        $start_time=$data->{"start_time"};
    
        $end_day=$data->{"select_end_day"};
        $end_time=$data->{"end_time"};
    
    $dowMap = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    
    $arradate = date('D');
    
    $arradateNum = array_search($arradate, $dowMap);
    
    $start_dayNum = array_search($start_day, $dowMap);
    
    $end_dayNum = array_search($end_day, $dowMap);
    
    if(
        ($arradateNum >= $start_dayNum && $nowtime>=$data->{"start_time"}) && 
        ($arradateNum <= $end_dayNum && $nowtime <= $data->{"end_time"})
    ) {
        echo "Show Anything";  
    }
    
    
    $dowMap=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
    $arradate=日期('D');
    $arradateNum=数组搜索($arradate,$dowMap);
    $start\u dayNum=array\u search($start\u day,$dowMap);
    $end\u dayNum=数组搜索($end\u day,$dowMap);
    如果(
    ($arradateNum>=$start\U dayNum&&$nowtime>=$data->{“start\U time”})和
    
    ($arradateNum您应该仔细阅读PHP手册,了解如何创建DateTime对象和各种可用的日期格式

    首先,您需要将用户输入转换为DateTimeObjects

    date_default_timezone_set('America/Toronto'); //Setting the Timezone
    //These are the User Inputs
    $start_day = 'tue';
    $start_time  = 181300;
    
    $end_day = 'fri';
    $end_time  = 134500;
    //User Input Ends
    
    //Create the Date Time Object for Start and End Date
    $startDate = DateTime::createFromFormat ( 'D His', $start_day . ' '. $start_time);
    $endDate = DateTime::createFromFormat ( 'D His', $end_day . ' '. $end_time);
    
    您可以在中检查createFromFormat的各种可用选项

    现在您有了日期-时间对象,您可以从中获取任何值。让我们以数字格式获取一周中的日期,以及以数字格式获取开始日期和结束日期的小时

    $startHour = $startDate->format('G');  //24-hour format of an hour without leading zeros
    $startDay = $startDate->format('N');  //numeric representation of the day of the week
    
    $endHour = $endDate->format('G'); //24-hour format of an hour without leading zeros
    $endDay = $endDate->format('N');  //numeric representation of the day of the week
    
    您可以查看
    G
    N
    格式以及中提供的其他选项

    我们需要检查时间是星期二下午6点到星期五下午6点之间

    首先,我们将检查这一天是否在星期二和星期五之间。我们有上面的
    $startDay
    $endDay
    变量来确定这一点。 对于星期二,对应的数值为2,对于星期五,对应的数值为5

    对于时间范围,我们有
    $startHour
    $endHour
    变量。格式中的6PM表示为18(12+6)

    因此,我们可以使用以下代码检查此情况:

    if ( $startDay >= 2 && $endDay <= 5 && $startHour >= 18 and $endHour <= 18 ) {
        echo 'In Range';
    } else {
        echo 'out of Range';
    }
    

    if($startDay>=2&&$endDay=18和$endHour这是我的尝试,使用映射表和字符串连接。如果日期顺序相反,则它不起作用。例如,如果今天是星期天,而
    选择开始日
    的值是
    周五
    选择结束日
    的值是
    周一
    ,则它不起作用

    <?php
    
    $arr = (object) [
        'select_start_day' => 'wed',
        'start_time' => 143400,
        'select_end_day' => 'wed',
        'end_time' => 220000
    ];
    
    $map_daysToNumbers = ['sun'=>1, 'mon'=>2, 'tue'=>3, 'wed'=>4, 'thu'=>5, 'fri'=>6, 'sat'=>7];
    
    $startString = $map_daysToNumbers[$arr->select_start_day] . str_pad($arr->start_time, 6, '0', STR_PAD_LEFT);
    $endString = $map_daysToNumbers[$arr->select_end_day] . str_pad($arr->end_time, 6, '0', STR_PAD_LEFT);
    
    $tz = new \DateTimeZone('America/Toronto');
    
    $today = new \DateTime('now', $tz);
    
    $todayString = ($today->format('w')+1) . $today->format('His');
    
    if($startString <= $todayString && $todayString <= $endString){
        echo 'In range';
    }
    

    这个问题的难点在于处理在周末“环绕”的范围,即您的示例案例5

    我建议建立一个涵盖两周的参考日数组

    $days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
    $days = array_merge($days, $days);
    
    对其进行切片,使其在开始点的当天开始(将从0重新编制索引)

    然后可以构建一个引用整数
    $nowDay = $arradate;
    $nowTime = $nowtime;
    
    $startDay = $start_day;
    $startTime = $start_time;
    
    $endDay = $end_day;
    $endTime = $end_time;
    
    class WeekDayTime
    {
        /** @var string[] map of the name of days and their number */
        const DAY_MAP = [
            'Mon' => 1,
            'Tue' => 2,
            'Wed' => 3,
            'Thu' => 4,
            'Fri' => 5,
            'Sat' => 6,
            'Sun' => 7
        ];
    
        /** @var int number of the day */
        private $dayNumber;
    
        /** @var int amount of hours */
        private $hours;
    
        /** @var int amount of minutes */
        private $minutes;
    
        /** @var int amount of seconds */
        private $seconds;
    
        /**
         * Constuctor
         * @param string $day number of the day
         * @param int $hours amount of hours
         * @param int $minutes amount of minutes
         * @param int $seconds amount of seconds
         */ 
        public function __construct(string $day, int $hours, int $minutes, int $seconds)
        {
            assert(array_key_exists($day, static::DAY_MAP), 'The day is incorrect');
            assert($hours < 24, 'The hours must be less than 24');
            assert($minutes < 60, 'The hours must be less than 60');
            assert($seconds < 60, 'The hours must be less than 60');
            $this->dayNumber = static::DAY_MAP[$day];
            $this->hours = $hours;
            $this->minutes = $minutes;
            $this->seconds = $seconds;
        }
    
        /**
         * Get number of the day
         * @return int number of the day
         */
        public function getDayNumber(): int
        {
            return $this->dayNumber;
        }
    
        /**
         * Get amount of hours
         * @return int amount of hours
         */
        public function getHours(): int
        {
            return $this->hours;
        }
    
        /**
         * Get amount of minutes
         * @return int amount of minutes
         */
        public function getMinutes(): int
        {
            return $this->minutes;
        }
    
         /**
         * Get amount of seconds
         * @return int amount of seconds
         */
        public function getSeconds(): int
        {
            return $this->seconds;
        }
    
        /**
         * Check if the current week day time is less the a denined week day time
         * @param WeekDayTime $value value which will be compared
         * @return bool status of the checking
         */
        public function isLessOrEqual(WeekDayTime $value): bool
        {
            $isLess = $this->dayNumber < $value->dayNumber;
            $isLessOrEqual = $this->dayNumber === $value->getDayNumber()
                && $this->hours <= $value->getHours()
                && $this->minutes <= $value->getMinutes()
                && $this->seconds <= $value->getSeconds();
            return $isLess || $isLessOrEqual;
        }
    
        /**
         * Check if the current week day time is greater the a denined week day time
         * @param WeekDayTime $value value which will be compared
         * @return bool status of the checking
         */
        public function isGreaterOrEqual(WeekDayTime $value): bool
        {
            $isGreater = $this->dayNumber > $value->dayNumber;
            $isGreaterOrEqual = $this->dayNumber === $value->getDayNumber()
                && $this->hours >= $value->getHours()
                && $this->minutes >= $value->getMinutes()
                && $this->seconds >= $value->getSeconds();
            return $isGreater || $isGreaterOrEqual;
        }
    }
    
    class WeekDayTimeRange
    {
        /** WeekDayTime range start */
        private $start;
    
        /** WeekDayTime range end */
        private $end;
    
        /**
         * Constuctor
         * @param WeekDayTime $start range start
         * @param WeekDayTime $end range end
         */
        public function __construct(WeekDayTime $start, WeekDayTime $end)
        {
            $this->start = $start;
            $this->end = $end;
        }
    
        /**
         * Check if a date-time occurs into the range
         * @param DateTimeInterface the date-time which will be checked
         * @return bool status of the checking
         */
        public function inRange(DateTimeInterface $dateTime): bool
        {}
    }
    
    public function inRange(DateTimeInterface $dateTime): bool
    {
        $day = $dateTime->format('D');
        $hours = $dateTime->format('H');
        $minutes = $dateTime->format('i');
        $seconds = $dateTime->format('s');
        $weekDayTime = new WeekDayTime($day, $hours, $minutes, $seconds);
    
        return $this->start->isLessOrEqual($weekDayTime) && $this->end->isGreaterOrEqual($weekDayTime);
    }
    
    public function inRange(DateTimeInterface $dateTime): bool
    {
        $day = $dateTime->format('D');
        $hours = $dateTime->format('H');
        $minutes = $dateTime->format('i');
        $seconds = $dateTime->format('s');
        $weekDayTime = new WeekDayTime($day, $hours, $minutes, $seconds);
    
        // if the range end is less then range start we break the current range to two range
        if ($this->end->isLessOrEqual($this->start))  {
            $range1 = new WeekDayTimeRange($this->start, new WeekDayTime('Sun', 23,59,59));
            $range2 = new WeekDayTimeRange(new WeekDayTime('Mon', 0,0,0), $this->end);
            return $range1->inRange($dateTime) || $range2->inRange($dateTime);
        }
    
        return $this->start->isLessOrEqual($weekDayTime) && $this->end->isGreaterOrEqual($weekDayTime);
    }
    
    // Date occurs into the range from Tuesday to Friday
    $start = new WeekDayTime('Tue', 10, 0,0);
    $end = new WeekDayTime('Fri', 14, 0,0);
    $range = new WeekDayTimeRange($start, $end);
    $range->inRange(DateTime::createFromFormat('Y-m-d H:i:s', '2019-10-03 10:00:00'));
    
    // Date doesn't occur into the range from Tuesday to Friday
    $start = new WeekDayTime('Tue', 10, 0,0);
    $end = new WeekDayTime('Fri', 14, 0,0);
    $range = new WeekDayTimeRange($start, $end);
    $range->inRange(DateTime::createFromFormat('Y-m-d H:i:s', '2019-10-05 10:00:00'));
    
    // Date doesn't occur into the range from Friday to Tuesday
    $start = new WeekDayTime('Fri', 14, 0,0);
    $end = new WeekDayTime('Tue', 10, 0,0);
    $range = new WeekDayTimeRange($start, $end);
    $range->inRange(DateTime::createFromFormat('Y-m-d H:i:s', '2019-10-03 10:00:00'));
    
    // Date occurs into the range from Friday to Tuesday
    $start = new WeekDayTime('Fri', 14, 0,0);
    $end = new WeekDayTime('Tue', 10, 0,0);
    $range->inRange(DateTime::createFromFormat('Y-m-d H:i:s', '2019-10-05 10:00:00'));
    
    date_default_timezone_set('America/Toronto');
    $now     = new DateTime(); 
    $date    = $now->format('Y-m-d');
    $time    = $now->format('h:i A');
    $dayname = strtolower(date('l', strtotime($date))); //it will give you today's day name
    
    
    $start_day = "monday"; //lets consider your data will be come as startday="monday" from $data->{"select_start_day"};
    $start_time = '07:50 AM'; //lets consider your data will be come as starttime="07:50 AM" from $data->{"start_time"}; 
    $end_day = "tuesday"; //lets consider your data will be come as endday="monday" from $data->{"select_end_day"};
    $end_time = '08:26 AM'; //lets consider your data will be come as endtime="08:26 AM" from $data->{"end_time"}; 
    
    $todays_date_time    = strtotime($dayname);
    $start_day_time      = strtotime($start_day);
    $end_day_time        = strtotime($end_day);
    $timeconvert         = strtotime($time);
    $timeconvertstart    = strtotime($start_time);
    $timeconvertend      = strtotime($end_time);
    
    if($todays_date_time >= $start_day_time && $todays_date_time <= $end_day_time ) 
     {
      if ($timeconvert >= $timeconvertstart && $timeconvert <= $timeconvertend) { 
         echo "test";
     }
    }