Hi I have this PHP code which I expect should be working however it is currently showing a blank page.
How can I get this working on my HTML site? As when I look on the php file on the local host it doesnt come up with anything just blank. What I want this for is so that different times of the day a different images appear, this is for a radio show. But I cant seem to get it working properly.
<?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.
// Adjust 2 hour offset for MST below.
$h = $h-2;
// MONDAY SCHEDULE
if ($d == 1 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h < 0) $img = 'img/hosts/test2.jpg';
// TUESDAY SCHEDULE
if ($d == 2 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h < 0) $img = 'img/hosts/test2.jpg';
// WEDNESDAY SCHEDULE
if ($d == 3 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h < 0) $img = 'img/hosts/test2.jpg';
// THURSDAY SCHEDULE
if ($d == 4 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h < 0) $img = 'img/hosts/test2.jpg';
// FRIDAY SCHEDULE
if ($d == 5 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h < 0) $img = 'img/hosts/test2.jpg';
you do not have a default image
you do not echo the image to the page at the end of the page
your schedules are the same for each day and your are setting the same image for every time
I would expect this:
http://sandbox.onlinephpfunctions.com/code/556ecb6463298fcfa82f93372a000b9738d911d9
<?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.
// Adjust 2 hour offset for MST below.
$h = $h-2;
$img="img/hosts/off_air.jpg"; // default
if ($h >= 12 && $h < 14) $img = "img/hosts/test$d_12to14.jpg";
else if ($h >= 14 && $h < 16) $img = "img/hosts/test$d_144to16.jpg";
else if ($h >=16 && $h < 18) $img = "img/hosts/test$d_16to18.jpg";
else if ($h >= 18 && $h < 20) $img = "img/hosts/test$d_18to20.jpg";
else if ($h >= 20 && $h < 22) $img = "img/hosts/test$d_20to22.jpg";
else if ($h >= 22 && $h < 24) $img = "img/hosts/test$d_22to24.jpg";
//else if ($h >= 19) $img = 'img/hosts/test2.jpg'; // already handled
else if ($h < 12) $img = 'img/hosts/test$d_morning.jpg';
echo "<img src=\"$img\" />";
?>
Now you can have images like
test1_12to14.jpg for Monday from 12 to 14
and
test3_22to24.jpg for Wednesday from 22 to 24
you have not declare the scope or condition for hour less than 12
your first condition should be
if ($d == day_number && $h < 12) $img = 'img/hosts/test2.jpg';
this is your code
Related
I am trying to have a web form use different times on the calendar based on if the form is a certain ID, if it is use the first group, if not use the default. I am hoping someone can help me here. I am a novice at php and intermediate at best in coding, I would appreciate any help. Thank you!
public function is_time_disabled($dt, $h, $m) {
$monday = 1;
$tuesday = 2;
$wensday = 3;
$thursday = 4;
$friday = 5;
$saturday = 6;
if (($form_id == 31)
return (
(($dt->format('w') == $monday && $h < 9) || ($dt->format('w') == $friday && $h > 17) || ($dt->format('w') == $friday && $h == 17 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);}
) if else return (
(($dt->format('w') == $monday && $h < 11) || ($dt->format('w') == $friday && $h > 18) || ($dt->format('w') == $friday && $h == 18 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
}
endif;))
This could be optimized (alot) and this is just a pointer in the right direction using your existing code, one funtion and two returns:
public function is_time_disabled($dt, $h, $m) {
$monday = 1;
$tuesday = 2;
$wensday = 3;
$thursday = 4;
$friday = 5;
$saturday = 6;
if ($form_id == 31) {
return (
(($dt->format('w') == $monday && $h < 9) || ($dt->format('w') == $friday && $h > 17) || ($dt->format('w') == $friday && $h == 17 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
} else {
return (
(($dt->format('w') == $monday && $h < 11) || ($dt->format('w') == $friday && $h > 18) || ($dt->format('w') == $friday && $h == 18 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
}
}
If you anticipate adding other forms then a switch might be better. If $form_id is not found in any of the case statements then default will be executed:
switch ($form_id) {
case 10:
//if the same as 31 then fall into the next one (no break no return)
case 31:
return (
(($dt->format('w') == $monday && $h < 9) || ($dt->format('w') == $friday && $h > 17) || ($dt->format('w') == $friday && $h == 17 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
case 99:
//return something else
default:
return (
(($dt->format('w') == $monday && $h < 11) || ($dt->format('w') == $friday && $h > 18) || ($dt->format('w') == $friday && $h == 18 && $m == 30) || ($dt->format('w') == $saturday && $h == 16 && $m == 30) || ($dt->format('w') == $saturday && $h > 16))
);
}
I have four greeting images which I intend to show based on what time user enters the site.
$morning = "img/morning.png";
$afternoon = "img/afternoon.png";
$evening = "img/evening.png";
$night = "img/night.png";
And I have some conditional statements to assign the values to $cover variable. When I tested, the conditional statement doesn't work.
date_default_timezone_set('Asia/Yangon');
$Hour = date('G');
if($Hour >= 5 && $Hour <= 11) {
$cover = $morning;
}elseif ($Hour >= 11 && $Hour <= 4) {
$cover = $afternoon;
}elseif ($Hour >= 4 && $Hour <= 9){
$cover = $evening;
}elseif ($Hour >= 9 && $Hour <= 4) {
$cover = $night;
}
Img tag
<img class="card-img-top" src="<?php echo $cover; ?>" alt="Not Available" >
if($Hour >= 0 && $Hour <= 11) {
$cover = $morning;
}
elseif ($Hour > 11 && $Hour <= 16) {
$cover = $afternoon;
}
elseif ($Hour > 16 && $Hour <= 19){
$cover = $evening;
}
else{
$cover = $night;
}
Above code will check your hours from 00:00 until 24:00 next day. I fixed your if-else statements so they make more sense in a way that there is a flow in the times.
G 24-hour format of an hour without leading zeros 0 through 23
$hour = date('H', time());
if( $hour > 6 && $hour <= 11) {
$cover = $morning;
}
else if($hour > 11 && $hour <= 16) {
$cover = $afternoon;
}
else if($hour > 16 && $hour <= 23) {
$cover = $evening;
}
else {
$cover = $night;
}
Construct an instance of date and acquire the hour from it
I'm trying to identify the current work shift from 3 options (24 hour format)
First shift 06:00 to 13:59
Second shift 14:00 to 21:59
Third shift 22:00 to 05:59
I tried this, but it's not working as expected
$hour = date("0500");
$shift;
if ($hour >= 0600 && $hour <= 1359 ) {
$shift = 1;
}else if($hour >= 14 && $hour <= 2159 )
{
$shift = 2;
}else
{
$shift = 3;
}
Maybe:
$hour = data('H');
if($hour >= 6 && $hour < 14) {
$shift = 1;
} else if($hour >= 14 && $hour < 22) {
$shift = 2;
} else {
$shift = 3;
}
You could try something like this perhaps:
$hour = data('H');
switch( true ){
case ( $hour >= 6 && $hours < 14 ):$shift=1; break;
case ( $hour >=14 && $hour < 22 ):$shift=2; break;
default: $shift=3; break;
}
Put your time in quotes otherwise starting with 0 makes it an octal number
Also stick with one format if you are going to be comparing
$hour = date("Hi", strtotime("05:00"));
$shift;
if ($hour >= "0600" && $hour <= "1359" ) {
$shift = 1;
}else if($hour >= "1400" && $hour <= "2159" ) {
$shift = 2;
}else {
$shift = 3;
}
I've just finished writing my script that changes an image depending on the time. It's all good except it's displaying the wrong image.
<?php
date_default_timezone_set('America/Los_Angeles');
$w = date('W'); # week
$d = date('N'); # day
$t = date('G'); # time
dealWithTime($d);
function dealWithTime($day) {
if ($w == 13) { # Week 13
if ($day == 1) {
# Monday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18) {
printImage('Midtown.png');
}
} else if ($day == 2) {
# Tuesday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18) {
printImage('Midtown.png');
}
} else if ($day == 3) {
# Wednesday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18) {
printImage('Midtown.png');
}
} else if ($day == 4) {
# Thursday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18) {
printImage('Midtown.png');
}
} else if ($day == 5) {
# Friday
if ($t >= 2 && $t < 8) {
printImage('Midtown.png');
} else if ($t >= 8 && $t < 12) {
printImage('Terminal.png');
} else if ($t >= 12 && $t < 16) {
printImage('XD_Holo.png');
} else if ($t >= 16 && $t < 20) {
printImage('Midtown.png');
} else if ($t >= 20) {
printImage('Terminal.png'); // SHOULD BE THIS ONE
}
} else if ($day == 6) {
# Saturday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18 && $t < 22) {
printImage('Midtown.png');
} else if($t >= 22) {
printImage('Terminal.png');
}
} else if ($day == 7) {
# Sunday
if ($t >= 2 && $t < 8) {
printImage('XD_Holo.png');
} else if ($t >= 8 && $t < 12) {
printImage('Midtown.png');
} else if ($t >= 12 && $t < 16) {
printImage('Terminal.png');
} else if ($t >= 16 && $t < 20) {
printImage('XD_Holo.png');
} else if ($t >= 20) {
printImage('Midtown.png');
}
}
} else if ($w == 14) { # Week 14
if ($day == 1) {
# Monday
if ($t >= 0 && $t < 6) {
printImage('Terminal.png');
} else if ($t >= 6 && $t < 10) {
printImage('XD_Holo.png');
} else if ($t >= 10 && $t < 14) {
printImage('Midtown.png');
} else if ($t >= 14 && $t < 18) {
printImage('Terminal.png');
} else if ($t >= 18 && $t < 22) {
printImage('XD_Holo.png');
} else if ($t >= 22) {
printImage('Midtown.png');
}
} else if ($day == 2) {
# Tuesday
if ($t >= 2 && $t < 8) {
printImage('Terminal.png');
} else if ($t >= 6 && $t < 10) {
printImage('XD_Holo.png');
} else if ($t >= 10 && $t < 14) {
printImage('Midtown.png');
} else if ($t >= 14 && $t < 18) {
printImage('Terminal.png');
} else if ($t >= 18) {
printImage('XD_Holo.png');
}
} else if ($day == 3) {
# Wednesday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
} else if ($day == 4) {
# Thursday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
} else if ($day == 5) {
# Friday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
} else if ($day == 6) {
# Saturday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
} else if ($day == 7) {
# Sunday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
}
} else { # else
printImage('fin.png');
}
}
function printImage($im) {
$file = $im;
$type = 'image/png';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
}
?>
As you scroll down, you should see a comment saying what image it should be (as of this post).
I also wrote another script to see if it was the time that was wrong but it's giving me the correct results.
<?php
date_default_timezone_set('America/Los_Angeles');
$w = date('W'); # week
$d = date('N'); # day
$t = date('G'); # time
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Week:</h2>
<span><?php echo $w; ?></span>
<h2>Day:</h2>
<span><?php echo $d; ?></span>
<h2>Time:</h2>
<span><?php echo $t; ?></span>
</body>
</html>
The first script can be seen here: http://spedwards.cz.cc/event/e.php
And the second here: http://spedwards.cz.cc/event/t.php
I will not be changing these files so they will stay relevant to the question for at least a month.
Could someone please explain to me why the correct image doesn't show? All 4 images are in the same directory.
You're not passing the variables into the function call.
For example:
dealWithTime($w, $d, $t);
function dealWithTime($w, $day, $t) {
The return value of the date format functions are strings, you are trying to evaluate them as integers.
You should convert the variables to int using (int) $d before evaluating it.
I'm using PHP to schedule images to come up on a website at a specific time. I'm fine until a picture has to come up say at 9:42am and end at 10:42am. a can get top of hours and partial hours ending at top of hour but I can't figure out how to schedule multiple partial hours in a row. This is what I'm using
date_default_timezone_set('America/Los_Angeles');
$h = date('G');
$m = date('i');
$d = date('w');
$year = date('Y');
// MONDAY SCHEDULE
if ($d == 1 && $h >= 0 && $h < 1) { $img = 'opendoor.jpg'; }
else if ($d == 1 && $h >= 13 && $h < 14) { $img = 'newtime.jpg'; }
else if ($d == 1 && $h >= 14 && $h < 15) { $img = 'weekend'; }
else if ($d == 1 && $h >= 15 && $h < 16) { $img = 'today.jpg'; }
else if ($d == 1 && $h >= 16 && $h <= 17 && $m >= 0 && $m < 30) { $img = 'walk.jpg'; }
else if ($d == 1 && $h >= 17 && $h < 19 && $m > 30) { $img = 'new.jpg'; }
else if ($d == 1 && $h >= 19 && $h < 20 && $m < 30) { $img = 'default.jpg'; }
else if ($d == 1 && $h >= 19 && $h < 20 && $m > 30) { $img = 'test.jpg'; }
I followed a very good example given to me for my question, but for no apparent reason, some of the hours work and some doesn't. When I echo $img on one hour the code is printed but on the hour before or after it is ommitted. Here is a partial code of what I'm now doing:
date_default_timezone_set('America/Los_Angeles');
$h = date('G');
$m = date('i');
$d = date('w');
$hhmm = date('H') * 100 + date("i");
$img = key(array_filter(array(
'images/hosts/image1.jpg' => ($d == 0 && $h >= 0 && $h < 1),
'images/hosts/image2.jpg' => ($d == 0 && $hhmm >= 0100 && $hhmm <= 0542 ),
'images/hosts/image3.jpg' => ($d == 0 && $hhmm >= 0542 && $hhmm <= 0600),
'images/hosts/image4' => ($d == 0 && $hhmm >= 0600 && $hhmm < 0630),
'images/hosts/image5' => ($d == 0 && $hhmm >= 0630 && $hhmm < 0800),
'images/hosts/image5.jpg' => ($d == 6 && $h >= 8 && $h < 9)
If you want to compare "partial hours", then it might be best to combine hours and minutes into one value. You can often get away with hours*100+minutes:
$h = date('G');
$m = date('i');
$d = date('w');
$hhmm = data("H") * 100 + date("i");
So $hhmm would be 1230 for 12:30am, or 1942 for 7:42pm. The numbers jump between 1759 and 1800, but that's not an issue since you only want to compare with <= and => within time ranges anyway.
Also I would totally rewrite the if/else chain into an array comparison:
$img = key(array_filter(array(
'opendoor.jpg' => ($d == 1 && $h >= 0 && $h < 1),
'newtime.jpg' => ($d == 1 && $h >= 13 && $h < 14),
...
'partialhours.jpg' => ($hhmm => 2142 && $hhmm <= 2242),
)));
please use switch
switch ($d)
{
case 1:
switch ($h)
{
case 1: $img = 'opendoor.jpg'; break;
case 13: $img = 'newtime.jpg'; break;
case 14: $img = 'weekend.jpg'; break;
case 15: $img = 'today.jpg'; break;
case 16:
if ($m<30) $img = 'walk.jpg';
break;
case 17:
$img = ($m>30) ? 'new.jpg':'walk.jpg';
break;
case 18:
if ($m>30) $img = 'new.jpg';
break;
case 19:
$img = ($m>30) ? 'test.jpg':'default.jpg';
break;
}
break;
}