Javascript by Day/Time
- Started
- Last post
- 12 Responses
- dMullins
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 some of 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 PHP or Javascript, that you could point me to?
Could I accomplish this with Javascript OR PHP?
Thanks.
- ismith0
I personally would do it with PHP, just because I can only imagine eventually you might want do do something more for a restaurant website that would just be easier with some kind of dynamic backend... then again, I've never really gotten down and dirty with JS and something as simple as this might be a pretty good use. How comfortable are you with either?
- dMullins0
I have no need for PHP for anything on this site, other than this one specific idea.
I am comfortable using either PHP or Javascript, as this is likely requires a very base-level understanding of either.
- welded0
Not to necessarily suggest PHP over Javascript but it needn't be any more complex than something like this:
<img src="/images/<?php echo date('l'); ?>.jpg" alt="<?php echo date('l'); ?>" /> (hopefully that code doesn't get mangled...)
Basically you can just have a series of images named 'Monday.jpg', 'Tuesday.jpg', etc. and when your page is parsed it'll just work. You can make it fancier and more complicated, but this'll get the job done with a minimum of fuss.
http://php.net/manual/en/functio…
- nocomply0
you might need something more robust than this, but I've done something similar using javascript to load different CSS files based on the time of day.
Just create a CSS id and assign different background images in each different css file.
- airey0
the other question is, will this be night and day according to the business timezone or the viewing browser? obviously if it's a simple script it will just look at the users computer / browser time and give that graphic whereas a more comprehensive solution will base the look on a specific timezone running on the hosting server.
- Would need it to be defined based on business timezone.dMullins
- acescence0
javascript date and time can be wrong if the user's computer's time or date are wrong.
- welded0
I don't know off hand if you can define the timezone in JS, but here's how you'd do it with PHP (because otherwise all date functions will use the server time):
date_default_timezone_set('Ameri...
http://www.php.net/manual/en/fun…
http://www.php.net/manual/en/tim…
- acescence0
i think php is more efficient overall, you get the page served up as it should be from the server, rather than page load > script load > script execute > image fetch > page render complete
- airey0
srry to hijack but can someone recommend a decent host for a client? i use mediatemple and can't refer clients to them anymore. something with good admin tools (like the mt ones), dns and database control as well as multiple domains under the one account. thx in advance.
- DreamHost is fine.welded
- fatcow.comutopian
- free.frspraycan
- dMullins0
@airey: www.asmallorange.com
- spraycan0
javascript no, because time is based on client's computer(the time of the operating system). you need to evaluate the server time.
- fugged0
Just make sure that when you get the time on the server, that you get UTC time. Then subtract/add the appropriate amount for the time-zone that the business is located in. Then then your code will work regardless of what time zone the server is in.
- Or do as I suggest and set the timezone at runtime.welded