James Lawson
James Lawson

Reputation: 419

Pulling in data around/after midnight

Ok.. so I've created myself a messy problem.

So I have a jquery slider that shows 5 slides of content

1) -2 hours ago

2) -1 hour ago

3) (0) present

4) +1 hours

5) +2 hours

The content for each slide is decided by what time of day it is. However when it comes to trying to go back/forwards 1 to 2 hours during the run up to midnight and after midnight the whole script breaks, and kills the site.

<?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 . '';

?>

As you can see it figures the time and then drops the correct file in - there's five of these for everyday of the week (-2.php, -1.php, 0.php, 1.php, 2.php).

Does anybody have a solution? Ideally I need to stop the break, but I don't want my visitors to be scrolling +1 / 2 or -1 / 2 hours on for the same days rotation when it nears, and steps over midnight.

For example, right now the code is broken on -2.php until at least 2am (my timezone) so that it back track.

I've totally burnt myself out trying to figure this one out.

Upvotes: 0

Views: 99

Answers (2)

user1864610
user1864610

Reputation:

The problems arising from the change of day can become intractable. I'd tackle this a different way. Start by calculating the day and hour for the five periods your interested in and use DateTime to do the heavy lifting. Then, use a function to provide the schedule item for a particular day/time combination.

Here's a 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 a complete schedule - you can work that out.

If you're using PHP 5.5 or later you can use DateTimeImmutable instead of DateTime which does away with all the cloning.

There's a fiddle here

Upvotes: 1

John Conde
John Conde

Reputation: 219814

Get rid of the leading zeros in your comparisons. Those are octal numbers and not decimals. 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';

Upvotes: 1

Related Questions