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
I am currently working on one project and come across a challenge that I am unable to resolve so far. I have tried long and hard and eventually putting my scenarios here as I can get some useful tips. Also, bear with me as I am going to give a whole details of the problem.
So, I have a project that contains the module which calculates the wages of their employees based on their shift timings (time is 24 hour format). Now, the pay rate is different according to the timings such as in week days (Monday to Friday) the pay rate is divided in two sections: Week Day and Week Night. Moreover, the weekend (Saturday & Sunday) pay rate is also different than normal days. Finally, there are public holidays and they also have different pay rates than rest of the events. Following table shows the summary of the pay rates:
------------------------------------------
|Event | Shift Times |
------------------------------------------
|Week Day | 0600-1800 |
------------------------------------------
|Week Night | 1800-0600 |
------------------------------------------
|Weekend | All day weekend rate |
-------------------------------------------
|Public holiday|All day public holiday rate|
-------------------------------------------+
Now, this looks really simple if you have to assign pay rates only for that particular day such as the shift is on public holiday then assign public holiday rates but the problem is this is not a case in my project.
How it works
Now, the shift is usually 12 hour long and can fall between two different pay rates such as one employee starts at 0900 and finishes 2100 on Monday. So, the calculations of hours would be like 9 hours on week day pay rate and 3 hours on week night pay rate. Similarly, the employee can do shift in a way that it can fall in three different pay rates like : shift starts at 2200 Sunday night and finishes at 0900 on the Monday morning. Therefore, the pay rate hours would be like 2 hours on weekend rate as the shift starts on Sunday night and and weekend end after 2 hours. So, from 0000 to 0600 (6 hours )the pay rate would be based on week night rate and rest of the hours comes under week day.
Now the good news is that I am able to do this up until now. Everything works perfect. Although I have put so many conditions inside it which i believe is not a good practice but for time being i want to make it run. The problem arise when Public Holiday happens. Now the public holiday is the most difficult part as it can come on any day (week day and weekends) and have to recheck in every condition and have to do the additions and subtractions of hours which is too complicated and not working. I am able to do the part if the whole shift falls on public holiday. However, when it falls more than one pay rate category then it becomes tough for me to solve the issue. I would not share the public holiday one code as the code is too complicated and you might get confused.However, I am going to share the code where other pay rate formats are working fine.
Note
I would advice you to not get any ideas from my code as i want your suggestions and you might run out of ideas if you look at my code.
function getHoursBreakDown($time1, $time2){
$hoursForShift = array(
"weekday" => 0,
"weeknight" => 0,
"saturday" => 0,
"sunday" => 0,
"public" => 0
);
$t1 = strtotime($time1);
$t2 = strtotime($time2);
$t1_Day = (int) date("N", $t1);
$t2_Day = (int) date("N", $t2);
$t1_Hour = (int) date("G", $t1);
$t2_Hour = (int) date("G", $t2);
$t1_Date = (int) date("j", $t1);
$t2_Date = (int) date("j", $t2);
$t1_Month = (int) date("n", $t1);
$t2_Month = (int) date("n", $t2);
$t1_Minutes = round(date("i", $t1) / 60, 2); // to substitute, extra
$t2_Minutes = round(date("i", $t2) / 60, 2); // to add, lost
// Working days
if ($t1_Day<6) {
if ($t1_Hour > $t2_Hour) $t2_Hour += 24;
for($i = $t1_Hour; $i< $t2_Hour; $i++){
if (($i>= 6 && $i < 18) || ($t2_Day < 6 && $i >= 30) ) {
$hoursForShift['weekday']++;
} else if ($t2_Day < 6 || $i < 24) {
$hoursForShift['weeknight']++;
} else if ($t2_Day == 6) {
$hoursForShift['saturday']++;
} else if ($t2_Day == 7) {
$hoursForShift['sunday']++;
}
}
$t2_Hour = ($t2_Hour > 23) ? $t2_Hour - 24: $t2_Hour;
// Deducting extra minutes from sign in time
if ($t1_Hour >= 6 && $t1_Hour < 18) $hoursForShift['weekday'] -= $t1_Minutes;
else if ($t1_Hour < 6 || $t1_Hour >= 18) $hoursForShift['weeknight'] -= $t1_Minutes;
// Adding lost minutes to sign out
if ($t2_Day < 6 && ($t2_Hour >= 6 && $t2_Hour < 18)){ $hoursForShift['weekday'] += $t2_Minutes; }
else if ($t2_Day < 6 && ($t2_Hour < 6 || $t2_Hour >= 18)) $hoursForShift['weeknight'] += $t2_Minutes;
// Adding lost minutes to sign out if, sign out on weekend
if ($t2_Day == 6) $hoursForShift['saturday'] += $t2_Minutes;
else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes;
} else {
//Weekends
if ($t1_Hour > $t2_Hour) $t2_Hour += 24;
for($i = $t1_Hour; $i< $t2_Hour; $i++){
if ($t2_Day == 1 && ($i%24) < 12) {
$hoursForShift['weeknight']++;
} else if ($t1_Day == 6 && $i < 24) {
$hoursForShift['saturday']++;
} else if ($t1_Day == 7 || ($t1_Day == 6 && $i >= 24)) {
$hoursForShift['sunday']++;
}
}
if ($t2_Day == 1) $hoursForShift['weeknight'] -= $t1_Minutes;
if ($t2_Day == 6) $hoursForShift['saturday'] -= $t1_Minutes;
else if ($t2_Day == 7) $hoursForShift['sunday'] -= $t1_Minutes;
if ($t2_Day == 1) $hoursForShift['weeknight'] += $t2_Minutes;
else if ($t2_Day == 6) $hoursForShift['saturday'] += $t2_Minutes;
else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes;
}
}
Good thing is that nothing is coming out from database. This problem is based on totally mind calculations and techniques and invloves pure PHP.
Please any help in this regard would be highly appreciated.
Thanks.
Huff !! After working long and hard and pulling my hairs all day eventually I am able to figure out the solution of the problem. Following function is the all the answer of the question I was looking for.
function getHoursBreakDown($time1, $time2){
$hoursForShift = array(
"weekday" => 0,
"weeknight" => 0,
"saturday" => 0,
"sunday" => 0,
"public" => 0
);
$t1 = strtotime($time1);
$t2 = strtotime($time2);
$t1_Day = (int) date("N", $t1);
$t2_Day = (int) date("N", $t2);
$t1_Hour = (int) date("G", $t1);
$t2_Hour = (int) date("G", $t2);
$t1_Date = (int) date("j", $t1);
$t2_Date = (int) date("j", $t2);
$t1_Month = (int) date("n", $t1);
$t2_Month = (int) date("n", $t2);
$t1_Year = (int) date("Y", $t1);
$t2_Year = (int) date("Y", $t2);
$t1_Minutes = round(date("i", $t1) / 60, 2); // to substitute, extra
$t2_Minutes = round(date("i", $t2) / 60, 2); // to add, lost
$start=explode(' ', $time1);
$finish=explode(' ', $time2);
$publicHolidays = array(
"2014-01-01",
"2014-01-27",
"2014-04-18",
"2014-04-19",
"2014-04-21",
"2014-04-25",
"2014-06-09",
"2014-08-17",
"2014-10-06",
"2014-11-14",
"2014-12-25",
"2014-12-26"
);
// Working days
if ($t1_Day<6 ) {
if ($t1_Hour > $t2_Hour) $t2_Hour += 24;
for($i = $t1_Hour; $i< $t2_Hour; $i++){
if (($i>= 6 && $i < 18) || ($t2_Day < 6 && $i >= 30 && $i< 42) ) {
$hoursForShift['weekday']++;
} else if ($t2_Day < 6 || $i < 24) {
$hoursForShift['weeknight']++;
} else if ($t2_Day == 6) {
$hoursForShift['saturday']++;
} else if ($t2_Day == 7) {
$hoursForShift['sunday']++;
}
}
$t2_Hour = ($t2_Hour > 23) ? $t2_Hour - 24: $t2_Hour;
// Deducting extra minutes from sign in time
if((!in_array($start[0], $publicHolidays)) && (!in_array($finish[0], $publicHolidays))) {
if ($t1_Hour >= 6 && $t1_Hour < 18) $hoursForShift['weekday'] -= $t1_Minutes;
else if ($t1_Hour < 6 || $t1_Hour >= 18) $hoursForShift['weeknight'] -= $t1_Minutes;
// Adding lost minutes to sign out
if ($t2_Day < 6 && ($t2_Hour >= 6 && $t2_Hour < 18)){ $hoursForShift['weekday'] += $t2_Minutes; }
else if ($t2_Day < 6 && ($t2_Hour < 6 || $t2_Hour >= 18)) $hoursForShift['weeknight'] += $t2_Minutes;
// Adding lost minutes to sign out if, sign out on weekend
if ($t2_Day == 6) $hoursForShift['saturday'] += $t2_Minutes;
else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes;
}
} else if($t1_Day>=6) {
//Weekends
if ($t1_Hour > $t2_Hour) $t2_Hour += 24;
for($i = $t1_Hour; $i< $t2_Hour; $i++){
if ($t2_Day == 1 && $i>=24 && $i<30) {
$hoursForShift['weeknight']++;
}else if ($t2_Day == 1 && $i>29 && $i<42) {
$hoursForShift['weekday']++;
}else if ($t1_Day == 6 && $i < 24) {
$hoursForShift['saturday']++;
} else if ($t1_Day == 7 || ($t1_Day == 6 && $i >= 24)) {
$hoursForShift['sunday']++;
}
}
if((!in_array($start[0], $publicHolidays)) && (!in_array($finish[0], $publicHolidays))) {
$t2_Hour = ($t2_Hour > 23) ? $t2_Hour - 24: $t2_Hour;
if ($t1_Day == 1 && $t1_Hour>=0 && $t1_Hour<6 ) $hoursForShift['weeknight'] -= $t1_Minutes;
else if ($t1_Day == 1 && $t1_Hour>=6 && $t1_Hour<18 ) $hoursForShift['weekday'] -= $t1_Minutes;
else if ($t1_Day == 6) $hoursForShift['saturday'] -= $t1_Minutes;
else if ($t1_Day == 7) $hoursForShift['sunday'] -= $t1_Minutes;
if ($t2_Day == 1 && $t2_Hour<6 || $t2_Day == 1 && $t2_Hour>=18) $hoursForShift['weeknight'] += $t2_Minutes;
else if ($t2_Day == 1 && $t2_Hour>=6 && $t2_Hour < 18) $hoursForShift['weekday'] += $t2_Minutes;
else if ($t2_Day == 6 ) $hoursForShift['saturday'] += $t2_Minutes;
else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes;
}
}
// If the start and end date is public holiday
if((in_array($start[0], $publicHolidays)) && (in_array($finish[0], $publicHolidays)))
{
$hoursForShift['public'] = $hoursForShift['weekday'];
$hoursForShift['weekday'] = 0;
$hoursForShift['public'] += $hoursForShift['weeknight'];
$hoursForShift['weeknight'] = 0;
$hoursForShift['public'] += $hoursForShift['saturday'];
$hoursForShift['saturday'] = 0;
$hoursForShift['public'] += $hoursForShift['sunday'];
$hoursForShift['sunday'] = 0;
$hoursForShift['public'] -= $t1_Minutes;
$hoursForShift['public'] += $t2_Minutes;
//If start date is on public holiday but the end date
}else if ((in_array($start[0], $publicHolidays)) && (! in_array($finish[0], $publicHolidays))) {
if ($t1_Hour > $t2_Hour) $t2_Hour += 24;
for($i = $t1_Hour; $i<24; $i++){
if($t1_Day<6){
if (($i>=6 && $i <18)) {
$hoursForShift['weekday']--;
$hoursForShift['public']++;
} else if ($i >=18 && $i <24) {
$hoursForShift['weeknight']--;
$hoursForShift['public']++;
}
else if ($i>0 && $i<=6) {
$hoursForShift['weeknight']--;
$hoursForShift['public']++;
}
} else if ($t1_Day == 6) {
$hoursForShift['saturday']--;
$hoursForShift['public']++;
} else if ($t1_Day == 7) {
$hoursForShift['sunday']--;
$hoursForShift['public']++;
}
}
$t2_Hour = ($t2_Hour > 23) ? $t2_Hour - 24: $t2_Hour;
$hoursForShift['public'] -= $t1_Minutes;
if ($t2_Day <6 && $t2_Hour>0 && $t2_Hour<6 || $t2_Day <6 && $t2_Hour>=18) $hoursForShift['weeknight'] += $t2_Minutes;
else if ($t2_Day <6 && $t2_Hour>=6 && $t2_Hour < 18) $hoursForShift['weekday'] += $t2_Minutes;
else if ($t2_Day == 6 ) $hoursForShift['saturday'] += $t2_Minutes;
else if ($t2_Day == 7) $hoursForShift['sunday'] += $t2_Minutes;
//If the start date is not on a public holiday but end date is.
}else if ((! in_array($start[0], $publicHolidays)) && (in_array($finish[0], $publicHolidays))) {
if ($t1_Hour > $t2_Hour) $t2_Hour += 24;
for($i = 25; $i<= $t2_Hour; $i++){
if($t2_Day<6){
if ( $i>24 && $i <=30 ) {
$hoursForShift['weeknight']--;
$hoursForShift['public']++;
} else if ($i >=30 && $i < 42 ) {
$hoursForShift['weekday']--;
$hoursForShift['public']++;
}
} else if ($t2_Day == 6) {
$hoursForShift['saturday']--;
$hoursForShift['public']++;
} else if ($t2_Day == 7) {
$hoursForShift['sunday']--;
$hoursForShift['public']++;
}
}
$hoursForShift['public'] += $t2_Minutes;
if ($t1_Day <6 && $t1_Hour>0 && $t1_Hour<6 || $t1_Day <6 && $t1_Hour>=18) $hoursForShift['weeknight'] -= $t1_Minutes;
else if ($t1_Day <6 && $t1_Hour>=6 && $t1_Hour < 18) $hoursForShift['weekday'] -= $t1_Minutes;
else if ($t1_Day == 6 ) $hoursForShift['saturday'] -= $t1_Minutes;
else if ($t1_Day == 7) $hoursForShift['sunday'] -= $t1_Minutes;
}
return $hoursForShift;
}
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 want to set $a = 5 between 09.00h and 16.59h, Monday to Friday.
$time = time()%86400;
if( (date("D") == "Mon") || (date("D") == "Tue") || (date("D") == "Wed") ||
(date("D") == "Thu") || (date("D") == "Fri") && $time >= 32400 && $time <= 61119){
$a = 5;
}
Have does the && time read at the end? Does it read "Mon OR Tue OR Wed OR Thu OR Fri..." AND "Time...", or instead "Fri AND Time"?
You're close, you'll need a set of parenthesis around the ORs, but a slightly cleaner way to write this might be:
if( in_array((date("D"), array("Mon","Tue","Wed","Thu","Fri"))
&& $time >= 32400 && $time <= 61119 ){
or even:
!in_array((date("D"), array("Sat","Sun"))
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;
}