Jump to content

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

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
dMullins

dMullins

    Newbie

  • Members
  • PipPip
  • 16 posts
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! :thumbup:

#2
dMullins

dMullins

    Newbie

  • Members
  • PipPip
  • 16 posts
Really surprised no answer yet.

#3
brokenbylaw

brokenbylaw

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Well you can do this in pure javascript, a basic function would be on the lines of
<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...

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

#4
dMullins

dMullins

    Newbie

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

THANKS! THANKS! THANKS! THANKS! THANKS!

#5
brokenbylaw

brokenbylaw

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
<?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

#6
dMullins

dMullins

    Newbie

  • Members
  • PipPip
  • 16 posts
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.

#7
dMullins

dMullins

    Newbie

  • Members
  • PipPip
  • 16 posts
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:

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?

#8
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
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:

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