php - Pulling in data around/after midnight -
ok.. i've created myself messy problem.
so have jquery slider shows 5 slides of content
1) -2 hours ago
2) -1 hour ago
3) (0) present
4) +1 hours
5) +2 hours
the content each slide decided time of day is. when comes trying go back/forwards 1 2 hours during run midnight , after midnight whole script breaks, , kills site.
<?php $h = date('g', strtotime ("-2 hour")); //set variable $h hour of day. $m = date('i', strtotime ("-2 hour")); //set variable $m min of hour. $d = date('w', strtotime ("-2 hour")); //set variable $d day of 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 . ''; ?>
as can see figures time , drops correct file in - there's 5 of these everyday of week (-2.php, -1.php, 0.php, 1.php, 2.php).
does have solution? ideally need stop break, don't want visitors scrolling +1 / 2 or -1 / 2 hours on same days rotation when nears, , steps on midnight.
for example, right code broken on -2.php until @ least 2am (my timezone) track.
i've totally burnt myself out trying figure 1 out.
the problems arising change of day can become intractable. i'd tackle different way. start calculating day , hour 5 periods interested in , use datetime
heavy lifting. then, use function provide schedule item particular day/time combination.
here's skeleton
<?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; } ?>
i haven't put in complete schedule - can work out.
if you're using php 5.5 or later can use datetimeimmutable instead of datetime away cloning.
there's fiddle here
Comments
Post a Comment