Calculate wages according to different pay rate format - php

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;
}

Related

Speeding up PHP script

I'm working on a calendar for my office. Every people has his own column and there is a line for every day.
There are some periodic date, where, for example, given people have to be working on the week-end. But most of the dates are coming from a MySQL database.
So it does a double loop (people vs dates) in which every dates have to be check for the person, what kind of occupation he has on this day.
Is there a way to optimize this script, because online it take at least 2-3 seconds (only 0.03 seconds according to PHP, but I don't feel like it's correct) and more than 8 seconds (again according to PHP) on our network! And this is just for 5 months, we'd like to have it for the whole year.
You can find a test version here (just to see the HTML and CSS): http://mybees.ch/for/tableau.php
And here is the PHP in it:
//Creating the line for the collaborators
$ids;
$ma_count=0;
$ma_name_query = mysqli_query($bdi,"SELECT DISTINCT m.id, m.name, m.vorname, m.grup FROM ma m, ma_pos mp WHERE m.id = mp.maid AND m.grup != 14 ORDER BY m.grup, mp.posid, m.name, m.vorname");
while($ma_name = mysqli_fetch_assoc($ma_name_query)) {
echo '<div class="cell name">'.$ma_name['name'].' '.$ma_name['vorname'].'</div>';
$ids[$ma_count] = $ma_name['id'];
$grup[$ma_count] = $ma_name['grup'];
$ma_count++;
}
// Check if special group day
$firstGroup = 1;
$firstDate = strtotime("$year-01-01 00:00 GMT");
$picket = array();
if (date("N", $firstDate) == 2)// Tuesday
$firstDate -= 24 * 3600;
elseif (date("N", $firstDate) == 3)// Wednesday
$firstDate -= 2 * 24 * 3600;
elseif (date("N", $firstDate) == 5)// Friday
$firstDate -= 24 * 3600;
elseif (date("N", $firstDate) == 6)// Saturday
$firstDate -= 2 * 24 * 3600;
elseif (date("N", $firstDate) == 7)// Sunday
$firstDate -= 3 * 24 * 3600;
for ($date = $firstDate; $date <= strtotime("$year-12-31 00:00 GMT"); $date += 24 * 3600) {
$weekNb = date("W",$date);
$weekDay = date("N",$date); // Monday = 1, Sunday = 7
if ($weekDay < 4)
$group = $weekNb % 4 - 1 + $firstGroup;
else
$group = $weekNb % 4 - 2 + $firstGroup;
if ($group == 0)
$group = 4;
if ($group == -1)
$group = 3;
$picket[$date] = $group;
}
$groupColor = ["yellow", "blue", "red", "green"];
function isPicket($date, $grup) {
global $picket, $groupColor;
//global $picket, $groupColor;
if ($grup < 5) {
if ($picket[$date] == $grup)
return " style='background-color: ".$groupColor[$picket[$date]-1]."'";
}
}
$today_stp = time() - time() % (24 * 3600) - 11 * 24 * 3600;
$frei_query_text = "SELECT id_ma, date1, date2, free.id as type, moment, remark, location FROM frei, free WHERE free.id = frei.type AND date1 BETWEEN CAST('$date1' AS DATE) AND CAST('$date2' AS DATE)";
$frei_query = mysqli_query($bdi, $frei_query_text);
$free = array();
while($frei = mysqli_fetch_assoc($frei_query)) {
$free[] = $frei;
}
// Filling the lines
for ($date = strtotime($date1); $date <= strtotime($date2); $date += 24 * 3600) {
$today = ($date == $today_stp) ? ' id="today"':'';
echo "<div class='clear'$today>";
$class = (date('N', $date) < 6) ? 'week' : 'weekend';
echo "<div class='date $class line'>".date("D, d.m.", $date)."</div>";
for ($i = 0; $i < $ma_count; $i++) {
echo "<div class='cell line $class'".isPicket($date,$grup[$i]).">
<div class='small-cell".isColor($ids[$i], $date, 0)."' id='divUp-".$ids[$i]."-$date'> </div>
<div class='small-cell".isColor($ids[$i], $date, 1)."' id='divDown-".$ids[$i]."-$date'> </div>
</div>";
}
echo '</div>';
}
function isColor($id_ma, $date, $moment) {
global $free;
for ($i = 0; $i < count($free); $i++) {
if ($id_ma == $free[$i]['id_ma']) {
if ($date >= strtotime($free[$i]['date1']) && $date <= strtotime($free[$i]['date2'])) {
if ($free[$i]['moment'] == $moment || $free[$i]['moment'] == 2) {
$type = $free[$i]['type'];
$style = "";
if ($type > 1 && $type < 5)
$style = " urlaub";
if ($type > 4 && $type < 8)
$style = " frei";
if ($type > 7 && $type < 11)
$style = " ferien";
if (($type > 22 && $type < 34) || ($type > 37 && $type < 48))
$style = " kurse";
if ($type > 10 && $type < 17)
$style = " krank";
return " $style' title='".$free[$i]['remark'];
}
}
}
}
}
Thank you very much for your help

Trying to show different banner images based on server time using PHP

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

How to know actual working shift with php

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;
}

eliminate weekends (sunday) by a php script

I am trying to count number of working days available for particular hours set. here i just need to exclude Sundays by the following php script. if this script find a Sunday this should increase the count. its working but,
This script is capable to exclude first 'Sunday' but not the 'second' and 'third'.
kindly give me a solution to correct this
function testRange() {
$phone_Quantity = 0;
$phone_Quantity = $_POST['phoneQuantity'];
if ($phone_Quantity > 0 && $phone_Quantity <= 300) {
return 300;
} elseif ($phone_Quantity >= 301 && $phone_Quantity <= 600) {
return 600;
} elseif ($phone_Quantity >= 601 && $phone_Quantity <= 900) {
return 900;
} elseif ($phone_Quantity >= 601 && $phone_Quantity <= 1200) {
return 1200;
} elseif ($phone_Quantity >= 1201 && $phone_Quantity <= 1500) {
return 1500;
} elseif ($phone_Quantity >= 1501 && $phone_Quantity <= 1800) {
return 1800;
}
}
echo testRange();
$query_to_get_hours = "SELECT
cdma_filtering_target_hours.Target_hours
FROM
cdma_filtering_target_hours
WHERE
cdma_filtering_target_hours.No_of_units='" . testRange() . "'
";
$query_to_get_hours_query = $system->prepareSelectQuery($query_to_get_hours);
foreach ($query_to_get_hours_query as $THours) {
$targeted_hours = $THours['Target_hours'];
}
$hid = 24; // Hours in a day - could be 24, 48, etc
$days = round($targeted_hours / $hid);
for ($xdays = 0; $xdays < $days; $xdays++) {
if (date('l', strtotime(date('y-m-d', strtotime("+$xdays days")))) == 'Sunday') {
$days++;
break;
}
}
Why do you converting +$xdays string representation twice?
If you comment your if statement and add next line
echo date('l', strtotime("+$xdays days"));
you can clearly see that it works.

PHP displaying wrong image

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.

Categories