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:
Day of the week, time of day script...
Started by dMullins, Mar 01 2010 12:59 PM
7 replies to this topic
#1
Posted 01 March 2010 - 12:59 PM
|
|
|
#2
Posted 01 March 2010 - 08:08 PM
Really surprised no answer yet.
#3
Posted 01 March 2010 - 08:35 PM
Well you can do this in pure javascript, a basic function would be on the lines of
that will output 1 because it is Monday, i don't think you need to assign them to the months except for aesthetics but...
that should output the day
now for the image, i guess you can do it by if and else
continuing from the past code
That code works in firefox :) i know there are better ways, I'm in a lazy mood sorry for crap
<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
Posted 01 March 2010 - 08:52 PM
Dude, thanks a bunch. I'll tinker with this tomorrow, and let you know if I have any questions.
THANKS! THANKS! THANKS! THANKS! THANKS!
THANKS! THANKS! THANKS! THANKS! THANKS!
#5
Posted 01 March 2010 - 08:53 PM
<?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
Posted 01 March 2010 - 09:02 PM
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.
Thanks a ton, I'm gonna try out the JS tomorrow, and let you know if I have any questions.
#7
Posted 02 March 2010 - 08:55 AM
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:
Any thoughts on this?
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
Posted 03 March 2010 - 02:04 AM
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:
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.
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
I study Information Systems at Karlstad University when I'm not on CodeCall


Sign In
Create Account


Back to top









