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.
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
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';
Related
Okay, how do I explain this. Let say I have an array of numbers/days.
$days = array(31,24,08,14,17);
Now I have 4 cycles with particular day range.
Cycle 1: 23-01
Cycle 2: 02-08
Cycle 3: 09-15
Cycle 4: 16-22
So now I want to count how many of the value from the days array fall on each cycle.
Cycle 1 will count as 2 since 31 and 24 falls within 23-01
Cycle 2 will count 1 (08)
Cycle 3 will count 1 (14)
Cycle 4 will count 1 (17)
The numbers are from days of a given date and I just have to count the days that fall in. I already can count from cycle 2-4 but having problem with cycle 1.
$cycle1 = 0;
$cycle2 = 0;
$cycle3 = 0;
$cycle4 = 0;
$days = array(31,24,08,14,17);
foreach ($days as $day)
{
if ($day >= 23 && $day <= 01)
{
$cycle1++;
}
if ($day >= 02 && $day <= 08)
{
$cycle2++;
}
if ($day >= 09 && $day <= 15)
{
$cycle3++;
}
if ($day >= 16 && $day <= 22)
{
$cycle4++;
}
}
Thanks, hope someone can shed some light on how to do it via only date('d',strtotime(datestring)); range.
<?php
$days = array(31,24,14,17,1,16);
/*
Cycle 2: 02-08 =>() = 0
Cycle 3: 09-15 =>(14) = 1
Cycle 4: 16-22 =>(17,16) = 2
Cycle 1: 23-01 =>(31,24,1) = 3
*/
$cycle1 = $cycle2 = $cycle3 = $cycle4 = 0;
foreach ($days as $day)
{
if($day >= 23)
$cycle1++;
elseif($day >= 16)
$cycle4++;
elseif($day >= 9)
$cycle3++;
elseif($day >= 2)
$cycle2++;
else
$cycle1++;
}
echo "cycle1=$cycle1<br>cycle2=$cycle2<br>cycle3=$cycle3<br>cycle4=$cycle4<br>";
I made this switch, but I dont know this last step (the ?? and the comments).
If someone can help me, thanks a lot!
<?
$nuDatumTijd = date("Y-m-d H:i:s");
$nuUur = date("H");
switch(true)
{
case $nuUur > 8 && $nuUur < 13:
$aantSecErbij = ??; // number of seconds untill the first next 13:00
$weerOp = date($nuDatumTijd,$aantSecErbij);
echo $weerOp;
break;
case $nuUur > 12 && $nuUur < 18:
$aantSecErbij = ??; // number of seconds untill the first next 19:00
$weerOp = date($nuDatumTijd,$aantSecErbij);
echo $weerOp;
break;
case $nuUur > 17 && $nuUur < 22:
$aantSecErbij = ??; // number of seconds untill the first next 9:00 (so that is the next day)
$weerOp = date($nuDatumTijd,$aantSecErbij);
echo $weerOp;
break;
}
?>
I now have fixed it this way, thanks for your input:
$nuUur = date("H");
if ($nuUur > 8 && $nuUur < 13)
{
$weerOp = strtotime('today 13:00');
$weerOpNetjes = date('Y-m-d H:i:s', $weerOp);
echo $weerOpNetjes;
}
elseif ($nuUur > 12 && $nuUur < 18)
{
$weerOp = strtotime('today 19:00');
$weerOpNetjes = date('H:i', $weerOp);
echo $weerOpNetjes;
}
elseif ($nuUur > 17 && $nuUur < 22)
{
$weerOp = strtotime('tomorrow 9:00');
$weerOpNetjes = date('H:i', $weerOp);
echo $weerOpNetjes;
}
switch-case can be used this way too, no problem about that.
$aantSecErbij = ??; // number of seconds untill the first next 13:00
If this means the number of seconds until today's 1pm then the following will get you the timestamp of today's 1pm.
$now = getdate();
$time1pm = mktime(13, 0, 0, $now['mon'], $now['mday'], $now['year']);
If it means the number of seconds between now and today's 1pm then you need to subtract with current timestamp.
$now = getdate();
$diff1pm = mktime(13, 0, 0, $now['mon'], $now['mday'], $now['year']) - time();
.
$aantSecErbij = ??; // number of seconds untill the first next 9:00 (so that is the next day)
For the next day you can add 1 to the day part and don't have to worry about the end of month. PHP will figure out the right date for you.
$now = getdate();
$tomorrow9am = mktime(9, 0, 0, $now['mon'], $now['mday'] + 1, $now['year']);
I'm trying to display an image based on the day of the month and the hour of the day. Here's the php code that I found via a forum and tweaked to meet my needs
<?php
$h = date('G'); //set variable $h to the hour of the day
$d = date('d'); //set variable $d to the day of the month.
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
// Adjust offset code if needed $h = $h-2;
// 01 Calendar Day
if ($d = 01 && $h >= 4 && $h < 12) $img = 'img/s1.jpg'; //if day is 1st and it's between 4am and 12pm show day strength 1 image
else if ($d == 01 && $h >= 12 && $h < 2) $img = 'img/c1.jpg'; //if day is 1st and it's between 12pm and 2am show evening condition 1 image
else if ($d == 01 && $h >= 2 && $h < 4) $img = 'img/rest.jpg'; //if day is 1st and it's between 2am and 4am show rest image
?>
<img src="/<?php echo $img; ?>">
The goal is to create if/else statements for all 31 possible days in a month, where there is a morning image, an evening image, and a buffer image that displays in the late night as a buffer.
But when i check the code to see if it works I'm getting errors. Please help and also if there's a more efficient way to code this verses 31 if/else statements that would be wonderful also.
You dont need to apply day condition as your image is not changing bases on day. It is changing on time bases. So you can use something like below:
$h = date('H'); // it will return hour in 24 format.
if ($h >= 4 && $h < 12) $img = 'img/s1.jpg'; //if it's between 4am and 12pm show day strength 1 image
else if ( ($h >= 12 && $h <= 23) || ($h >= 1 && $h <= 2) ) $img = 'img/c1.jpg'; //it's between 12pm and 2am show evening condition 1 image
else if ($h >= 2 && $h < 4) $img = 'img/rest.jpg'; //if it's between 2am and 4am show rest image
You can make changes in your arithmetical operators as per your requirement. And if your image changes on day bases, you can simply add date in your image variable as below:
$h = date('H'); // it will return hour in 24 format.
$d = date('d');
if ($h >= 4 && $h < 12) $img = 'img/s'.$d.'.jpg';
else if ( ($h >= 12 && $h <= 23) || ($h >= 1 && $h <= 2) ) $img = 'img/c'.$d.'.jpg';
else if ($h >= 2 && $h < 4) $img = 'img/rest.jpg';
Hope it helps you.
Try This,
$h = date('G'); //set variable $h to the hour of the day
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
$d = date('d'); //set variable $d to the day of the month.
$DynamicDay = '21';
if ($DynamicDay == $d) {
switch ($h) {
case ($h >= 4 && $h < 12):
$img = 'img/s' . $d . '.jpg';
break;
case ($h >= 12 && $h < 14):
$img = 'img/c1.jpg';
break;
case ($h >= 14 && $h < 18):
$img = 'img/rest.jpg';
break;
default:
break;
}
}
echo 'Hour => '.$h.'<p>';
echo 'day => '.$d.'<p>';
echo $img;
die;
Out Put:
Hour => 10
day => 21
img/s21.jpg
Instead of multiple if/else, you can put all the rule into array for easy to maintain.
$array = [
1 => [
4 => 'img/s1.jpg',
12 => 'img/s12.jpg',
14 => 'img/s14.jpg',
],
2 => [
2 => 'img/c2.jpg'
]
];
function getDayDateImage($array, $d, $h)
{
while (!$array[$d][$h]) {
$h--;
if ($h < 0) {
$h = 23;
$d--;
}
if ($d < 1) {
return 'img/rest.jpg'; // Default
}
}
return $array[$d][$h];
}
echo getDayDateImage($array, 1, 1); // Get img/rest.jpg
echo getDayDateImage($array, 1, 13); // Get img/s12.jpg
echo getDayDateImage($array, 2, 1); // Get img/s14.jpg
echo getDayDateImage($array, 2, 4); // Get img/c2.jpg
//Just pass you $d and $h
//getDayDateImage($array, $d, $h);
Hope this help.
I have this code bellow it works fine but when the time hits 24 till 1 it doesn't work. I'm not sure why.
The code I have:
<?php
$h = date('G'); //set variable $h to the hour of the day
$d = date('w'); //set variable $d to the day of the week.
$year = date('Y'); //set variable $year to the current year
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
// MONDAY SCHEDULE
if ($d == 1 && $h >= 0 && $h < 4) $text= 'bob';
else if ($d == 1 && $h >= 22 && $h < 23) $text= 'adam';
else if ($d == 1 && $h >= 23 && $h < 24) $text= 'tina';
else if ($d == 2 && $h < 0) $img = 'mark';
// TUESDAY SCHEDULE
if ($d == 2 && $h >= 0 && $h < 4) $text= 'alex';
else if ($d == 2 && $h >= 4 && $h < 8) $text= 'jason';
else if ($d == 3 && $h < 0) $text= 'gorge';
print $text;
?>
When it's mark's or gorge's turns it doesn't work. I would like to know how to fix it. Any help would be appreciated.
Thank you
Like stated in comments, the test on $h < 0 will always return false, as $h is an integer between 0 and 23.
But you could make your schedule more readable and manageable if you would create a data structure for it, like this:
mb_internal_encoding("UTF-8");
$persons = array(
'-' => '(no one)',
'a' => 'Bob',
'b' => 'Adam',
'c' => 'Tina',
'd' => 'Marc',
'e' => 'Alex',
'f' => 'Jason',
'g' => 'George'
);
$schedule = array(
// 012345678901234567890123
'------------------------', // Sunday
'aaaa------------------bc', // Monday
'eeeeffff----------------', // Tuesday
'------------------------', // Wednesday
'------------------------', // Thursday
'------------------------', // Friday
'------------------------', // Saturday
);
// Use mb_substr to support multi-byte characters (unicode)
print $persons[mb_substr($schedule[date('w')],date('G'),1)];
In the above code, each person gets a unique letter. That letter is used in a kind of grid, where each row represents a weekday, and each column (character position) an hour of the day (0-23).
This way you have a rather visual representation, which might be easier to manage. And as it turns out, to get the person that is scheduled for the current hour-slot just takes a single line of code.
This is my code below to get. but this code will invalidate the hour before 6PM which minute is above 30, how do I do it that
if today is "3" and today hour is still before 6:30PM , the if condition will works
My current code:
if( ($today=="3") && ($today_hour < "18") && ($today_min < "30") )
{
}
You should only check the minutes if the hour is actually 6. Change your condition to:
if ($today == 3 && ($today_hour < 18 || ($today_hour == 18 && $today_min < 30))) {
// Do stuff
}
Or easier to read:
$beforeTime = $today_hour < 18 || ($today_hour == 18 && $today_min < 30);
if ($today == 3 && $beforeTime) {
// Do stuff
}
Make sure to work with numbers as well, because doing string comparison on them will lead to unexpected results when the amount of digits is different.
Try this it will help you :
if ((date('d') == "03") && (date('H') < "18:30")) {
$condition = true;
}
H = 24-hour format of an hour (00 to 23)