2015-07-11 59 views
0

好的..所以我创造了一个混乱的问题。拉起午夜后的数据

所以,我有一个jquery滑块显示的内容5个滑动

1)-2小时前

2)-1小时前

3)(0)本

4)+1小时

5)+2小时

eac的内容h幻灯片是由一天中的什么时间决定的。然而,在试图在午夜到午夜之间尝试返回/前进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点(我的时区),以便它回溯。

我已经竭尽全力想办法解决这个问题。

回答

1

由于日子的变化而产生的问题会变得棘手。我会以不同的方式解决这个问题。首先计算您感兴趣的五个时间段的日期和时间,并使用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; 
} 

?> 

我没有放在一个完整的时间表 - 你可以工作了这一点。

如果您使用的是PHP 5.5或更高版本,则可以使用DateTimeImmutable而不是DateTime来完成所有克隆。

有一个小提琴here

1

摆脱比较中的前导零。这些是八进制数字而不是小数。 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';