Closed Thread
Results 1 to 8 of 8

Thread: Day of the week, time of day script...

  1. #1
    dMullins is offline Newbie
    Join Date
    Aug 2009
    Location
    North Carolina
    Posts
    16
    Rep Power
    0

    Day of the week, time of day script...

    I've got a small side-project I'm working on, and was wondering if you guys could point me in the right direction on something I want to accomplish. I am not 100% if this is something I *SHOULD* do with PHP, or if I could accomplish it with JavaScript/jQuery instead. I don't know if I want the whole site wrapped in PHP because of this one feature, but you guys are the experts, so I'll revert to your best opinions.

    I'm doing a site for a restaurant my buddy owns, and the idea I have is that I want different graphics to show on the home page, based on time of day, or day of the week. For example, if it's Monday, the graphic will be a picture of empty tables, with copy that says, "CLOSED ON MONDAYS." Similarly, if you go to the site on a Sunday, it would show an image/copy that is based around their Sunday Jazz brunches.

    I was Googling around for a script or framework to control this sort of thing, but kept getting terrible search returns—I think mostly due to bad search phrases I was using, as I tended to only get countdown timers and such in my Google results.

    Do any of you guys have a recommendation for something like this, or even a particular out-of-the-box script, that you could point me to?

    Thanks, I really appreciate the community here!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    dMullins is offline Newbie
    Join Date
    Aug 2009
    Location
    North Carolina
    Posts
    16
    Rep Power
    0

    Re: Day of the week, time of day script...

    Really surprised no answer yet.

  4. #3
    brokenbylaw's Avatar
    brokenbylaw is offline Learning Programmer
    Join Date
    Dec 2009
    Posts
    62
    Rep Power
    0

    Re: Day of the week, time of day script...

    Well you can do this in pure javascript, a basic function would be on the lines of
    Code:
    <script type="text/javascript">
    
    var date = new Date();
    document.write(date.getDay());
    
    </script>
    that will output 1 because it is Monday, i don't think you need to assign them to the months except for aesthetics but...

    Code:
    <script type="text/javascript">
    
    var day=new Date();
    var weekday=new Array(7);
    weekday[0]="Sunday";
    weekday[1]="Monday";
    weekday[2]="Tuesday";
    weekday[3]="Wednesday";
    weekday[4]="Thursday";
    weekday[5]="Friday";
    weekday[6]="Saturday";
    
    document.write("Today is " + weekday[day.getDay()]);
    
    </script>
    that should output the day

    now for the image, i guess you can do it by if and else
    continuing from the past code
    Code:
    <img src="" ID="image" border="0" />
    <script type="text/javascript">
    
    var day=new Date();
    var weekday=new Array(7);
    weekday[0]="Sunday";
    weekday[1]="Monday";
    weekday[2]="Tuesday";
    weekday[3]="Wednesday";
    weekday[4]="Thursday";
    weekday[5]="Friday";
    weekday[6]="Saturday";
    var x = weekday[day.getDay()];
    
    if(x == "Monday"){
        document.getElementById('image').src = "monday.png";
    
    }
    
    </script>
    That code works in firefox i know there are better ways, I'm in a lazy mood sorry for crap

  5. #4
    dMullins is offline Newbie
    Join Date
    Aug 2009
    Location
    North Carolina
    Posts
    16
    Rep Power
    0

    Re: Day of the week, time of day script...

    Dude, thanks a bunch. I'll tinker with this tomorrow, and let you know if I have any questions.

    THANKS! THANKS! THANKS! THANKS! THANKS!

  6. #5
    brokenbylaw's Avatar
    brokenbylaw is offline Learning Programmer
    Join Date
    Dec 2009
    Posts
    62
    Rep Power
    0

    Re: Day of the week, time of day script...

    Code:
    <?php
    $img = imagecreatetruecolor(200,100);
    
    $black = imagecolorallocate($img, 0,0,0);
    $blue = imagecolorallocate($img, 46,103,173);
    
    imagefill($img,0,0, $black);
    $day = date("l");
    imagettftext($img, 40, 0, 15, 60, $blue, "font.ttf", $day);
    
    header("content-type: image/png");
    imagepng($img);
    imagedestroy($img);
    
    ?>
    i still bored that's php, it makes an image with the date printed on it... it was fun

  7. #6
    dMullins is offline Newbie
    Join Date
    Aug 2009
    Location
    North Carolina
    Posts
    16
    Rep Power
    0

    Re: Day of the week, time of day script...

    I may end up opting for Javascript, as there's really no need for me to wrap the whole site in PHP is not necessary.

    Thanks a ton, I'm gonna try out the JS tomorrow, and let you know if I have any questions.

  8. #7
    dMullins is offline Newbie
    Join Date
    Aug 2009
    Location
    North Carolina
    Posts
    16
    Rep Power
    0

    Re: Day of the week, time of day script...

    Based on the way Javascript works (client-side), I will have to use PHP in order to most accurately determine the relevant time to use. I don't want users in another time zone to see that the restaurant is closed, if in fact the restaurant is open.

    So I copied out your PHP work, slapped it into a .PHP file, threw it up on my server, and got this error message:

    Code:
    Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/dylanm/public_html/sandy/index.php on line 9
    ‰PNG  IHDRÈdLäè\PIDATxœíÁ‚ ÿ¯nH@üêÄ•|—bIEND®B`‚
    Any thoughts on this?

  9. #8
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: Day of the week, time of day script...

    That PHP code is just a plain example of drawing a picture with the days name on it, it's not what you want.

    you need something like this:

    Code:
    switch(date("l")) {
        case 
    0: {
            
    // do sunday stuff
            
    break;
        }
        case 
    1: {
            
    // do monday stuff
            
    break;
        }
    // here you do the same with cases 2-6 for tuesday to saturday

    Please note that I can't know what you want to do on each day, so you need to fix that code lines starting with // for each weekday.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. 1 Week soon
    By Kill3r in forum Introductions
    Replies: 4
    Last Post: 06-13-2010, 09:34 PM
  2. Out for a week
    By WingedPanther in forum The Lounge
    Replies: 5
    Last Post: 02-17-2010, 03:33 PM
  3. Calendar with Week number and week view!
    By Hamed in forum PHP Development
    Replies: 4
    Last Post: 09-25-2009, 03:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

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