Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Image Change Based on Day/Time - Code Being Stubborn

    Hello everyone, so to make a long story short. I'm using a code that displays an image based on the Day and Time. Everything runs smoothly for the most part. Except around 9pm. For example on Friday at 9pm no image appeared, the spot was blank as if something should be there but there wasn't. At 10pm the correct image appeared. Tonight at 9pm the wrong image appeared. It was supposed to be one radio host when instead a different host appeared. I'm lost as to what the problem is since the rest of the hours seem to be working correctly (or at least they do when I have time to monitor the site.) Here is the code I have for those two days in particular....

    PHP Code:
    <?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
    // Adjust 4 hour offset for EST below.
    $h $h-4;

    // FRIDAY SCHEDULE
    if ($d == && $h >= && $h 1$img '/images/shows/DennisPrager.png';
    else if (
    $d == && $h >= && $h 6$img '/images/shows/RedEyeRadio.png';
    else if (
    $d == && $h >= && $h 10$img '/images/shows/Local.png';
    else if (
    $d == && $h >= 10 && $h 12$img '/images/shows/Music.png';
    else if (
    $d == && $h >= 12 && $h 15$img 'images/shows/MariluHenner.png';
    else if (
    $d == && $h >= 15 && $h 16$img '/images/shows/ShannonJoy.png';
    else if (
    $d == && $h >= 16 && $h 19$img '/images/shows/JoyBrowne.png';
    else if (
    $d == && $h >= 19 && $h 20$img '/images/shows/BillNojay.png';
    else if (
    $d == && $h >= 20 && $h 21$img '/images/shows/TMZ.png';
    else if (
    $d == && $h >= 21 && $h 22$img '/images/shows/ShannonJoy.png';
    else if (
    $d == && $h >= 22$img '/images/shows/DennisPrager.png';
    else if (
    $d == && $h 0$img '/images/shows/DennisPrager.png';

    // SATURDAY SCHEDULE
    if ($d == && $h >= && $h 1$img '/images/shows/DennisPrager.png';
    else if (
    $d == && $h >= && $h 6$img '/images/shows/RedEyeRadio.png';
    else if (
    $d == && $h >= && $h 8$img '/images/shows/HaidtReport.png';
    else if (
    $d == && $h >= && $h 9$img '/images/shows/ABCNews.png';
    else if (
    $d == && $h >= && $h 12$img '/images/shows/GarySullivan.png';
    else if (
    $d == && $h >= 12 && $h 14$img '/images/shows/PopularTech.png';
    else if (
    $d == && $h >= 14 && $h 15$img '/images/shows/WhatWorks.png';
    else if (
    $d == && $h >= 15 && $h 17$img '/images/shows/JillMoney.png';
    else if (
    $d == && $h >= 17 && $h 19$img '/images/shows/YouManual.png';
    else if (
    $d == && $h >= 19 && $h 20$img '/images/shows/MadeAmerica.png';
    else if (
    $d == && $h >= 20 && $h 21$img '/images/shows/MensHealth.png';
    else if (
    $d == && $h >= 21 && $h 22$img '/images/shows/AlanTaylor.png';
    else if (
    $d == && $h >= 22$img '/images/shows/HughHewitt.png';
    else if (
    $d == && $h 0$img '/images/shows/HughHewitt.png';
    ?>

    <img src="<?php echo $img?>">

    Any help or advice would be greatly appreciated. My uneducated guess would be that its the last 2-3 lines of code for each day that is screwing things up. I kinda guessed on how to end the day. This is how it should end, for example Saturday should be

    Alan Taylor 9pm - 10pm
    Hugh Hewitt 10pm - Midnight.


    Thanks to anyone who can help!

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    You aren't taking into account that although you're subtracting the hours for time difference, you aren't doing the same for the days or year either. If you wish the image to change across timezones, then you might want to look into using javascript. If it's always the same, meaning eastern time should show picture A at noon, and central would show this at 11, then just go off the original time you got from the server instead of changing it. If you do this, then images you want to show at 5PM you would set to 9PM of the day you want.
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes I want the image to be the same no matter which time zone the person is on. So like you said the 5pm Eastern Image should show up in California at 1pm over there.

    I think changing everything based off the server (I'm located in NY) would somehow screw me up (which doesnt take much lol) Is there anyway to adjust the server time? I'm a bit of a noob when it comes to PHP to the easier the better I suppose.

    Thanks for your help.

  • #4
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also all the other times during the day work correctly. Its just the last few hours. If it was an issue with the days and years wouldn't the rest of the times not work either? Or am I just over thinking things?

  • #5
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,228
    Thanks
    10
    Thanked 270 Times in 269 Posts
    This is a curious piece of code:
    PHP Code:
    // Adjust 4 hour offset for EST below.
    $h $h-4
    Since you're subtracting 4 hours, that means your server is 4 hours east of you - in Europe? In any event, on Friday 9pm you have Shannon Joy on, right? At that time, its 1am the next day in Europe. You subtract 4 hours from the hour, leaving -3 o'clock, not 21 o'clock.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    Your code is likely outputting something, even if the date/time calculation is not correct. I'm guessing you have a typo or a capitalization problem in an image name. When you do a 'view source' of the page in your browser, what do you see and is the path/filename in the <img tag correct?

    You can set the timezone that php uses for the date() function - PHP: date_default_timezone_set - Manual

    You shouldn't hard-code what you are doing, because you are both likely making a set of code for each day of the week, resulting in a huge amount of code, and a huge amount of code takes a huge amount of time to test and find things like typo's, and this style of coding requires that for any change you make, you must go into the code, find the values you want to change and actually modify the logic, without messing it up.

    What you need to do is put this data into an array with the day number, start time, end time, image name, and/or program name. You would then write relatively simple code to find the correct entry in the array that corresponds to the current day/time. Changing anything would simply involve changing the array holding the data, the program logic wouldn't get touched. Writing the code like this would also lead to the least amount of changes if/when you migrate the data to a database, which you should be using, as the code needed to get the program information out of a database for any given date/time is ridiculously simple.
    Last edited by CFMaBiSmAd; 06-08-2014 at 05:37 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    408
    Thanks
    18
    Thanked 26 Times in 26 Posts
    You can change the timezone of the server, however I'd recommend to not do this, most servers run on UTC time, so that way it's all the same, that way if you ever have any information transferring between servers, you don't get timezones mixed up.

    There are other functions that allow you to work with a different timezone, I'd recommend those instead. When you're programming, you need to learn to adjust, even if you don't like it right away. I was the same way you are now, I wanted my servers to be in my timezone, but then it throws things off more for everything else. In my opinion, it's easier to learn to adjust now rather than fight with mixed data later.

    If you want to convert the times from UTC, look into php's DateTime() class.
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •