午夜 around/after 提取数据

Pulling in data around/after midnight

好吧..所以我给自己制造了一个混乱的问题。

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

1) -2 小时前

2) -1 小时前

3) (0) 存在

4) +1 小时

5) +2 小时

每张幻灯片的内容取决于一天中的什么时间。然而,当涉及到在 运行 期间尝试去 back/forwards 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 点(我的时区),以便它返回轨道。

为了解决这个问题,我已经筋疲力尽了。

去掉比较中的前导零。这些是八进制数而不是小数。 You won't get the results you expect.

// 09 is not a valid octal number. It gets converted to zero in decimal.
else if ($d == 6 && $h >= 07 && $h < 09)
..
else if ($d == 6 && $h >= 09 && $h < 12) $file ='throughthemorning.php';

日期变化引起的问题会变得棘手。我会以不同的方式解决这个问题。首先计算您感兴趣的五个时段的日期和时间,然后使用 DateTime 完成繁重的工作。然后,使用函数为特定 day/time 组合提供计划项目。

这是骨架

<?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;
}

?>

我还没有制定完整的时间表 - 你可以算出来。

如果您使用的是 PHP 5.5 或更高版本,您可以使用 DateTimeImmutable 而不是 DateTime,这样可以避免所有克隆。

有个fiddlehere