Php 午夜前后拉入数据

Php 午夜前后拉入数据,php,datetime,time,Php,Datetime,Time,好的。。所以我给自己制造了一个麻烦 所以我有一个jquery滑块,它显示了5张内容幻灯片 1) -2小时前 2) -1小时前 3) (0)出席 4) +1小时 5) +2小时 每张幻灯片的内容由一天中的时间决定。然而,在午夜之前和午夜之后的1到2个小时内,当试图返回/转发时,整个脚本都会中断,并杀死该站点 <?php $h = date('G', strtotime ("-2 hour")); //set variable $h to the hour of the day. $m =

好的。。所以我给自己制造了一个麻烦

所以我有一个jquery滑块,它显示了5张内容幻灯片

1) -2小时前

2) -1小时前

3) (0)出席

4) +1小时

5) +2小时

每张幻灯片的内容由一天中的时间决定。然而,在午夜之前和午夜之后的1到2个小时内,当试图返回/转发时,整个脚本都会中断,并杀死该站点

<?php 

$h = date('G', strtotime ("-2 hour")); //set variable $h to the hour of the day.
$m = date('i', strtotime ("-2 hour")); //set variable $m to the min of the hour.
$d = date('w', strtotime ("-2 hour")); //set variable $d to the day of the week.

// SATURDAY SCHEDULE
if ($d == 6 && $h >= 0 && $h < 07) $file ='earlyhours.php';
else if ($d == 6 && $h >= 07 && $h < 09) $file ='breakfast.php';
else if ($d == 6 && $h >= 09 && $h < 12) $file ='throughthemorning.php';
else if ($d == 6 && $h >= 12 && $h < 13) $file ='rewind.php';
else if ($d == 6 && $h >= 13 && $h < 17 && $m <= 30) $file ='nonstop.php';
else if ($d == 6 && $h >= 17 && $m >= 30 && $h <21) $file ='livetrend.php';
else if ($d == 6 && $h >= 17 && $m >= 35 && $h < 21) $file ='nonstop.php';
else if ($d == 6 && $h >= 21 && $h < 18) $file ='gennation.php';
else if ($d == 6 && $h >= 23) $file ='earlyhours.php';
else if ($d == 7 && $h >= 0) $file ='earlyhours.php';

require $_SERVER['DOCUMENT_ROOT'] . '/collection/profiles/' . $file . '';

?>

如您所见,它计算时间,然后放入正确的文件-一周中每天有五个文件(-2.php,-1.php,0.php,1.php,2.php)

有人有解决办法吗?理想情况下,我需要停止休息,但我不希望我的访客在临近的同一天滚动+1/2或-1/2小时,然后在午夜时滚动

例如,现在代码在-2.php上被中断,直到至少凌晨2点(我的时区),以便它返回到原来的轨道


我想弄明白这一点,已经筋疲力尽了。

去掉比较中的前导零。这些是八进制数,不是小数

//09不是有效的八进制数。它被转换成十进制的零。
如果($d==6&&$h>=07&&$h<09),则为else
..
如果($d==6&&$h>=09&&$h<12)$file='throughthemorning.php';

日新月异带来的问题可能变得棘手。我会用另一种方式解决这个问题。首先计算您感兴趣的五个时段的日期和时间,然后使用
DateTime
进行繁重的工作。然后,使用函数为特定的日期/时间组合提供日程项目

这是一具骷髅

<?php
date_default_timezone_set('Pacific/Auckland');
$now = new DateTime();
$oneHour = new DateInterval('PT1H');
$minusOne =  (clone $now);
$minusOne->sub($oneHour);
$minusTwo =  (clone $minusOne);
$minusTwo->sub($oneHour);
$plusOne =   (clone $now);
$plusOne->add($oneHour);
$plusTwo =   (clone $plusOne);
$plusTwo->add($oneHour);

echo returnFile($minusTwo);
echo returnFile($minusOne);
echo returnFile($now);
echo returnFile($plusOne);
echo returnFile($plusTwo);

function returnFile(DateTime $t) {
    $day = $t->format('D');
    $hour = $t->format('G');
    // echo "Day:$day, Hour: $hour;<br>";
    switch ($day) {
        case 'Mon':
            if ($hour<7) {
                // Small hours Monday...
                $filename = "smallMonday.html";
                break;
            }
            if ($hour<12) {
               // Monday morning
                $filename = "morningMonday.html";
                break;
            }
            break;
        case 'Tue':
            if ($hour >=23) {
                // Late Tuesday
                $filename = "lateTuesday.html";
            }
        default:
            $filename = "Some other time";

    }
    return $filename;
}

?>

我还没有制定一个完整的时间表,你可以解决这个问题

如果您使用的是PHP5.5或更高版本,您可以使用DateTimeImmutable而不是DateTime,这将消除所有克隆

有把小提琴

<?php
date_default_timezone_set('Pacific/Auckland');
$now = new DateTime();
$oneHour = new DateInterval('PT1H');
$minusOne =  (clone $now);
$minusOne->sub($oneHour);
$minusTwo =  (clone $minusOne);
$minusTwo->sub($oneHour);
$plusOne =   (clone $now);
$plusOne->add($oneHour);
$plusTwo =   (clone $plusOne);
$plusTwo->add($oneHour);

echo returnFile($minusTwo);
echo returnFile($minusOne);
echo returnFile($now);
echo returnFile($plusOne);
echo returnFile($plusTwo);

function returnFile(DateTime $t) {
    $day = $t->format('D');
    $hour = $t->format('G');
    // echo "Day:$day, Hour: $hour;<br>";
    switch ($day) {
        case 'Mon':
            if ($hour<7) {
                // Small hours Monday...
                $filename = "smallMonday.html";
                break;
            }
            if ($hour<12) {
               // Monday morning
                $filename = "morningMonday.html";
                break;
            }
            break;
        case 'Tue':
            if ($hour >=23) {
                // Late Tuesday
                $filename = "lateTuesday.html";
            }
        default:
            $filename = "Some other time";

    }
    return $filename;
}

?>