Thayer2Design Posted November 10, 2014 Share Posted November 10, 2014 Hey everyone, So I've been using this code for a while and it may not be the best way of doing things but its worked for me based on my very limited knowledge of PHP. The code is based for a radio station which makes an image change based on the day and time. For some reason though due to daylight savings time ending the code is now off an hour. Whats the best way of fixing this without changing every single number? (Which I actually did try and still seem to be screwing it up) Below is the code: <p> <?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 // SUNDAY SCHEDULE if ($d == 0 && $h >= 4 && $h < 5) $img = '/images/shows/HughHewitt.png'; else if ($d == 0 && $h >= 5 && $h < 10) $img = '/images/shows/RedEyeRadio.png'; else if ($d == 0 && $h >= 10 && $h < 11) $img = '/images/shows/MomTalk.png'; else if ($d == 0 && $h >= 11 && $h < 12) $img = '/images/shows/GoodParenting.png'; else if ($d == 0 && $h >= 12 && $h < 14) $img = '/images/shows/PetShow.png'; else if ($d == 0 && $h >= 14 && $h < 15) $img = '/images/shows/GardenRebel.png'; else if ($d == 0 && $h >= 15 && $h < 16) $img = '/images/shows/WorkingMother.png'; else if ($d == 0 && $h >= 16 && $h < 17) $img = '/images/shows/WhatsCooking.png'; else if ($d == 0 && $h >= 17 && $h < 18) $img = '/images/shows/HomeWizards.png'; else if ($d == 0 && $h >= 18 && $h < 19) $img = '/images/shows/DougStephan.png'; else if ($d == 0 && $h >= 19 && $h < 20) $img = '/images/shows/Finance.png'; else if ($d == 0 && $h >= 20 && $h < 21) $img = '/images/shows/PopularScience.png'; else if ($d == 0 && $h >= 21 && $h < 22) $img = '/images/shows/ABCRadio.png'; else if ($d == 0 && $h >= 22) $img = '/images/shows/Medicine.png'; else if ($d == 1 && $h >= 0 && $h < 3) $img = '/images/shows/ArmedAmerica.png'; else if ($d == 1 && $h >= 3 && $h < 4) $img = '/images/shows/HughHewitt.png'; // MONDAY SCHEDULE if ($d == 1 && $h >= 4 && $h < 5) $img = '/images/shows/HughHewitt.png'; else if ($d == 1 && $h >= 5 && $h < 10) $img = '/images/shows/RedEyeRadio.png'; else if ($d == 1 && $h >= 10 && $h < 13) $img = '/images/shows/BobRick.png'; else if ($d == 1 && $h >= 13 && $h < 16) $img = '/images/shows/DougStephan.png'; else if ($d == 1 && $h >= 16 && $h < 19) $img = '/images/shows/MariluHenner.png'; else if ($d == 1 && $h >= 19 && $h < 20) $img = '/images/shows/DebbieNigro.png'; else if ($d == 1 && $h >= 20 && $h < 22) $img = '/images/shows/DaveRamsey.png'; else if ($d == 1 && $h >= 22) $img = '/images/shows/JoyBrowne.png'; else if ($d == 2 && $h >= 0 && $h < 1) $img = '/images/shows/ShannonJoy.png'; else if ($d == 2 && $h >= 1 && $h < 2) $img = '/images/shows/BillNojay.png'; else if ($d == 2 && $h >= 2 && $h < 4) $img = '/images/shows/DennisPrager.png'; // TUESDAY SCHEDULE if ($d == 2 && $h >= 4 && $h < 5) $img = '/images/shows/DennisPrager.png'; else if ($d == 2 && $h >= 5 && $h < 10) $img = '/images/shows/RedEyeRadio.png'; else if ($d == 2 && $h >= 10 && $h < 13) $img = '/images/shows/BobRick.png'; else if ($d == 2 && $h >= 13 && $h < 16) $img = '/images/shows/DougStephan.png'; else if ($d == 2 && $h >= 16 && $h < 19) $img = '/images/shows/MariluHenner.png'; else if ($d == 2 && $h >= 19 && $h < 20) $img = '/images/shows/DebbieNigro.png'; else if ($d == 2 && $h >= 20 && $h < 22) $img = '/images/shows/DaveRamsey.png'; else if ($d == 2 && $h >= 22) $img = '/images/shows/JoyBrowne.png'; else if ($d == 3 && $h >= 0 && $h < 1) $img = '/images/shows/ShannonJoy.png'; else if ($d == 3 && $h >= 1 && $h < 2) $img = '/images/shows/BillNojay.png'; else if ($d == 3 && $h >= 2 && $h < 4) $img = '/images/shows/DennisPrager.png'; // WEDNESDAY SCHEDULE if ($d == 3 && $h >= 4 && $h < 5) $img = '/images/shows/DennisPrager.png'; else if ($d == 3 && $h >= 5 && $h < 10) $img = '/images/shows/RedEyeRadio.png'; else if ($d == 3 && $h >= 10 && $h < 13) $img = '/images/shows/BobRick.png'; else if ($d == 3 && $h >= 13 && $h < 16) $img = '/images/shows/DougStephan.png'; else if ($d == 3 && $h >= 16 && $h < 19) $img = '/images/shows/MariluHenner.png'; else if ($d == 3 && $h >= 19 && $h < 20) $img = '/images/shows/DebbieNigro.png'; else if ($d == 3 && $h >= 20 && $h < 22) $img = '/images/shows/DaveRamsey.png'; else if ($d == 3 && $h >= 22) $img = '/images/shows/JoyBrowne.png'; else if ($d == 4 && $h >= 0 && $h < 1) $img = '/images/shows/ShannonJoy.png'; else if ($d == 4 && $h >= 1 && $h < 2) $img = '/images/shows/BillNojay.png'; else if ($d == 4 && $h >= 2 && $h < 4) $img = '/images/shows/DennisPrager.png'; // THURSDAY SCHEDULE if ($d == 4 && $h >= 4 && $h < 5) $img = '/images/shows/DennisPrager.png'; else if ($d == 4 && $h >= 5 && $h < 10) $img = '/images/shows/RedEyeRadio.png'; else if ($d == 4 && $h >= 10 && $h < 13) $img = '/images/shows/BobRick.png'; else if ($d == 4 && $h >= 13 && $h < 16) $img = '/images/shows/DougStephan.png'; else if ($d == 4 && $h >= 16 && $h < 19) $img = '/images/shows/MariluHenner.png'; else if ($d == 4 && $h >= 19 && $h < 20) $img = '/images/shows/DebbieNigro.png'; else if ($d == 4 && $h >= 20 && $h < 22) $img = '/images/shows/DaveRamsey.png'; else if ($d == 4 && $h >= 22) $img = '/images/shows/JoyBrowne.png'; else if ($d == 5 && $h >= 0 && $h < 1) $img = '/images/shows/ShannonJoy.png'; else if ($d == 5 && $h >= 1 && $h < 2) $img = '/images/shows/BillNojay.png'; else if ($d == 5 && $h >= 2 && $h < 4) $img = '/images/shows/DennisPrager.png'; // FRIDAY SCHEDULE if ($d == 5 && $h >= 4 && $h < 5) $img = '/images/shows/DennisPrager.png'; else if ($d == 5 && $h >= 5 && $h < 10) $img = '/images/shows/RedEyeRadio.png'; else if ($d == 5 && $h >= 10 && $h < 13) $img = '/images/shows/BobRick.png'; else if ($d == 5 && $h >= 13 && $h < 16) $img = '/images/shows/DougStephan.png'; else if ($d == 5 && $h >= 16 && $h < 19) $img = '/images/shows/MariluHenner.png'; else if ($d == 5 && $h >= 19 && $h < 20) $img = '/images/shows/DebbieNigro.png'; else if ($d == 5 && $h >= 20 && $h < 22) $img = '/images/shows/DaveRamsey.png'; else if ($d == 5 && $h >= 22) $img = '/images/shows/JoyBrowne.png'; else if ($d == 6 && $h >= 0 && $h < 1) $img = '/images/shows/ShannonJoy.png'; else if ($d == 6 && $h >= 1 && $h < 2) $img = '/images/shows/BillNojay.png'; else if ($d == 6 && $h >= 2 && $h < 4) $img = '/images/shows/DennisPrager.png'; // SATURDAY SCHEDULE if ($d == 6 && $h >= 4 && $h < 5) $img = '/images/shows/DennisPrager.png'; else if ($d == 6 && $h >= 5 && $h < 10) $img = '/images/shows/RedEyeRadio.png'; else if ($d == 6 && $h >= 5 && $h < 10) $img = '/images/shows/RedEyeRadio.png'; else if ($d == 6 && $h >= 10 && $h < 12) $img = '/images/shows/HaidtReport.png'; else if ($d == 6 && $h >= 12 && $h < 13) $img = '/images/shows/ABCNews.png'; else if ($d == 6 && $h >= 13 && $h < 16) $img = '/images/shows/GarySullivan.png'; else if ($d == 6 && $h >= 16 && $h < 18) $img = '/images/shows/PopularTech.png'; else if ($d == 6 && $h >= 18 && $h < 19) $img = '/images/shows/WhatWorks.png'; else if ($d == 6 && $h >= 19 && $h < 21) $img = '/images/shows/JillMoney.png'; else if ($d == 6 && $h >= 21 && $h < 23) $img = '/images/shows/YouManual.png'; else if ($d == 6 && $h >= 23) $img = '/images/shows/MadeAmerica.png'; else if ($d == 0 && $h >= 0 && $h < 1) $img = '/images/shows/MensHealth.png'; else if ($d == 0 && $h >= 1 && $h < 2) $img = '/images/shows/AlanTaylor.png'; else if ($d == 0 && $h >= 2 && $h < 4) $img = '/images/shows/HughHewitt.png'; ?> <img src="<?php echo $img; ?>"> Thank You! Kevin Quote Link to comment https://forums.phpfreaks.com/topic/292386-daylight-savings-time-screwed-up-code/ Share on other sites More sharing options...
requinix Posted November 10, 2014 Share Posted November 10, 2014 Have you already checked the time on the server? Quote Link to comment https://forums.phpfreaks.com/topic/292386-daylight-savings-time-screwed-up-code/#findComment-1496219 Share on other sites More sharing options...
Thayer2Design Posted November 10, 2014 Author Share Posted November 10, 2014 Unfortunately I used Godaddy Shared hosting (I know, I know) so I guess thats not an option Quote Link to comment https://forums.phpfreaks.com/topic/292386-daylight-savings-time-screwed-up-code/#findComment-1496224 Share on other sites More sharing options...
requinix Posted November 10, 2014 Share Posted November 10, 2014 Use date_default_timezone_set to make sure your code is using the right timezone. Are you open to the idea of refactoring that code? To make it easier to maintain, read nicer, not have as much copy-and-paste... It wouldn't fix the problem but it would be a good thing to do if you have the time. Quote Link to comment https://forums.phpfreaks.com/topic/292386-daylight-savings-time-screwed-up-code/#findComment-1496225 Share on other sites More sharing options...
Thayer2Design Posted November 10, 2014 Author Share Posted November 10, 2014 I'd be open to the idea but Id have no idea how to do it lol PHP isn't really my think and just doing that code took me a long time to figure out. But I'm open to the idea certainly. Also Ill check out what you sent me. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/292386-daylight-savings-time-screwed-up-code/#findComment-1496226 Share on other sites More sharing options...
mac_gyver Posted November 10, 2014 Share Posted November 10, 2014 ideally, you would store the data in a database, one row for each day/time-range combination and run a query to get the matching program information. lacking a database, you can use an array to hold the data. example code - <?php // define constants for day names to make reading and writing code easier define('SUN',0); define('MON',1); define('TUE',2); define('WED',3); define('THU',4); define('FRI',5); define('SAT',6); // extract common information (i.e. DRY - Don't Repeat Yourself) $path = '/images/shows/'; // data - days (one/array), start (inclusive), end (exclusive), img $a[] = array(SUN,0,1,'MensHealth'); //1 $a[] = array(SUN,1,2,'AlanTaylor'); //1 $a[] = array(SUN,2,5,'HughHewitt'); //1 (used later w/different hours) $a[] = array(range(SUN,SAT),5,10,'RedEyeRadio'); //7 $a[] = array(SUN,10,11,'MomTalk'); //1 $a[] = array(SUN,11,12,'GoodParenting'); //1 $a[] = array(SUN,12,14,'PetShow'); //1 $a[] = array(SUN,14,15,'GardenRebel'); //1 $a[] = array(SUN,15,16,'WorkingMother'); //1 $a[] = array(SUN,16,17,'WhatsCooking'); //1 $a[] = array(SUN,17,18,'HomeWizards'); //1 $a[] = array(SUN,18,19,'DougStephan'); //1 $a[] = array(SUN,19,20,'Finance'); //1 $a[] = array(SUN,20,21,'PopularScience'); //1 $a[] = array(SUN,21,22,'ABCRadio'); //1 $a[] = array(SUN,22,24,'Medicine'); //1 $a[] = array(MON,0,3,'ArmedAmerica'); //1 $a[] = array(MON,3,5,'HughHewitt'); //1 $a[] = array(range(MON,FRI),10,13,'BobRick'); //5 $a[] = array(range(MON,FRI),13,16,'DougStephan'); //5 $a[] = array(range(MON,FRI),16,19,'MariluHenner'); //5 $a[] = array(range(MON,FRI),19,20,'DebbieNigro'); //5 $a[] = array(range(MON,FRI),20,22,'DaveRamsey'); //5 $a[] = array(range(MON,FRI),22,24,'JoyBrowne'); //5 $a[] = array(range(TUE,SAT),0,1,'ShannonJoy'); //5 $a[] = array(range(TUE,SAT),1,2,'BillNojay'); //5 $a[] = array(range(TUE,SAT),2,5,'DennisPrager'); //5 $a[] = array(SAT,10,12,'HaidtReport'); //1 $a[] = array(SAT,12,13,'ABCNews'); //1 $a[] = array(SAT,13,16,'GarySullivan'); //1 $a[] = array(SAT,16,18,'PopularTech'); //1 $a[] = array(SAT,18,19,'WhatWorks'); //1 $a[] = array(SAT,19,21,'JillMoney'); //1 $a[] = array(SAT,21,23,'YouManual'); //1 $a[] = array(SAT,23,24,'MadeAmerica'); //1 $h = date('G'); //set variable $h to the hour of the day. $d = date('w'); //set variable $d to the day of the week. // find the image that matches the day/hr $img = ''; foreach($a as $e){ if(((is_array($e[0]) && in_array($d,$e[0])) || (!is_array($e[0]) && $d == $e[0])) && $h >= $e[1] && $h < $e[2] ){ $img = $e[3]; break; } } if($img == ''){ echo 'no matching program found'; } else { ?> <img src="<?php echo $path.$img.'.png'; ?>"> <?php } edit: don't you already have this information stored in a data structure someplace? how do you display the full schedule? Quote Link to comment https://forums.phpfreaks.com/topic/292386-daylight-savings-time-screwed-up-code/#findComment-1496230 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.