 |
 |
Help with a simple PHP script
|
 |
|
 |
|
Junior Member
Join Date: Feb 2004
Location: Portland ME
Status:
Offline
|
|
I'm looking to add a simple PHP script to the website of a client of mine, but I don't know much about the language, so I thought I'd ask you for some help.
I would like the script to figure out the current time, date, and day of the week on the East coast of the US, and from that information, figure out whether the restaurant (my client) is open, or when they will next be open, and display that information on the home page.
Is this even a job for PHP? Any suggestions on how to go about doing this?
Sorry to be dumb.
|
|
Hi. Nice to see you. You look lovely.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Mar 2002
Location: Clogland
Status:
Offline
|
|
Originally posted by AWShuttleworth:
Is this even a job for PHP? Any suggestions on how to go about doing this?
Php will handle it for sure, don't let this freak you out.....
http://nl2.php.net/manual/en/function.strftime.php
On the contrary.
Might get a little trickier if you want to adapt your script for daylight saving as it goes, but php is definately the way to go.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally posted by AWShuttleworth:
I'm looking to add a simple PHP script to the website of a client of mine, but I don't know much about the language, so I thought I'd ask you for some help.
I would like the script to figure out the current time, date, and day of the week on the East coast of the US, and from that information, figure out whether the restaurant (my client) is open, or when they will next be open, and display that information on the home page.
Is this even a job for PHP? Any suggestions on how to go about doing this?
Sorry to be dumb.
Sounds pretty easy, and a good use of PHP. Here's something I threw together. No doubt it could be optimized, but I was going for easy maintenance and readability.
[php]
<?php
$open_close_times = array();
// each day has its array(open_time, close_time) hours, 24-hour time
$open_close_times["Monday"] = array(8, 22);
$open_close_times["Tuesday"] = array(8, 22);
$open_close_times["Wednesday"] = array(8, 22);
$open_close_times["Thursday"] = array(8, 22);
$open_close_times["Friday"] = array(8, 23);
$open_close_times["Saturday"] = array(8, 24);
$open_close_times["Sunday"] = array(8, 24);
$current_hour = date("G", time());
$current_day = date("l", time());
$hours_today = $open_close_times[$current_day];
if ( ( $hours_today[0] <= $current_hour ) && ($current_hour <= $hours_today[1]) )
{
$close_time = mktime($open_close_times[$current_day][1], 0, 0, date("m"), date("d")+1, date("Y"));
$real_close_hour = date("g A", $close_time);
$message = "We're Open until $real_close_hour on $current_day";
}
else
{
// go to the next day, and see when we open
$tomorrow = date("l", mktime(0, 0, 0, date("m"), date("d")+1, date("Y"))); // day of the week tomorrow
$tomorrow_open_date = mktime($open_close_times[$tomorrow][0], 0, 0, date("m"), date("d")+1, date("Y"));
$time_open = date("g A", $tomorrow_open_date);
$message = "We open $tomorrow at $time_open";
}
?>
Welcome to Joe's Restaurant
<p>
<?php echo "$message" ?>
<p>
[/php]
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Feb 2004
Location: Portland ME
Status:
Offline
|
|
Wow, thanks a lot. I'm sure that once I get deeper into this that link and that script are going to be exceedingly helpful. I can't quite read them yet but I'm getting there
One more question: is there any application that can help me write PHP code, à la ActionScript palette in Flash?
|
|
Hi. Nice to see you. You look lovely.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
|
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Feb 2004
Location: Portland ME
Status:
Offline
|
|
Well, after spending a lot of time at the PHP website, I've finally written some code that seems to do what I need it to do. I haven't cleaned it up or merged it with my CSS layout, yet, but if you're interested, this is what I've come up with.
It's not much, but considering that I didn't know a thing about the language a week ago, I'm pleased with myself! And I hand-coded all of it using Hyperedit.
Thanks for your help. I'm sure I'll be back asking for more of it.
[php]
Welcome to Mims Brasserie. Thanks for visiting.<br>
<br>
<?php
putenv ("TZ=EST5EDT");
//get day and time
$day = strftime("%u");
$time = strftime("%H%M");
//set hours
//monday-thursday
$MRhours = array();
$MRhours["beginam"] = "0700" ;
$MRhours["endam"] = "1059" ;
$MRhours["beginmd"] = "1100" ;
$MRhours["endmd"] = "1459" ;
$MRhours["beginsoups"] = "1500" ;
$MRhours["endsoups"] = "1659" ;
$MRhours["beginpm"] = "1700" ;
$MRhours["endpm"] = "2100" ;
//friday
$Fhours = array();
$Fhours["beginam"] = "0700" ;
$Fhours["endam"] = "1059" ;
$Fhours["beginmd"] = "1100" ;
$Fhours["endmd"] = "1459" ;
$Fhours["begincocktails"] = "1500" ;
$Fhours["endcocktails"] = "1659" ;
$Fhours["beginpm"] = "1700" ;
$Fhours["endpm"] = "2200" ;
//saturday
$Shours = array();
$Shours["beginbrunch"] = "0900" ;
$Shours["endbrunch"] = "1459" ;
$Shours["begincocktails"] = "1500" ;
$Shours["endcocktails"] = "1659" ;
$Shours["beginpm"] = "1700" ;
$Shours["endpm"] = "2200" ;
//sunday
$SUhours = array();
$SUhours["beginbrunch"] = "0900" ;
$SUhours["endbrunch"] = "1459" ;
$SUhours["beginsoups"] = "1500" ;
$SUhours["endsoups"] = "1659" ;
$SUhours["beginpm"] = "1700" ;
$SUhours["endpm"] = "2100" ;
//decide which set of hours to use
if ( $day >= "1" and $day <= "4" )
{
$hourstoday = $MRhours ;
$daytype = "weekday" ;
}
if ( $day == "5" )
{
$hourstoday = $Fhours ;
$daytype = "weekday" ;
}
if ( $day == "6" )
{
$hourstoday = $Shours ;
$daytype = "weekend" ;
}
if ( $day == "7" )
{
$hourstoday = $SUhours ;
$daytype = "weekend" ;
}
//are we open?
//set status to closed so that it may be unset
$status = "closed" ;
if ( $daytype == "weekend" )
{
if ( $hourstoday["beginbrunch"] >= $time )
{
$status = "not yet open" ;
}
}
if ( $daytype == "weekday" )
{
if ( $hourstoday["beginam"] >= $time )
{
$status = "not yet open" ;
}
}
if ( ( $hourstoday["beginbrunch"] <= $time ) && ( $hourstoday["endbrunch"] >= $time ) )
{
$status = "brunch" ;
}
if ( ( $hourstoday["beginam"] <= $time ) && ( $hourstoday["endam"] >= $time ) )
{
$status = "am" ;
}
if ( ( $hourstoday["beginmd"] <= $time ) && ( $hourstoday["endmd"] >= $time ) )
{
$status = "md" ;
}
if ( ( $hourstoday["beginsoups"] <= $time ) && ( $hourstoday["endsoups"] >= $time ) )
{
$status = "soups" ;
}
if ( ( $hourstoday["begincocktails"] <= $time ) && ( $hourstoday["endcocktails"] >= $time ) )
{
$status = "cocktails" ;
}
if ( ( $hourstoday["beginpm"] <= $time ) && ( $hourstoday["endpm"] >= $time ) )
{
$status = "pm" ;
}
//figure out closed status
if ( $status == "closed")
{
$tomorrow = ( $day + 1 ) ;
if ( $tomorrow >= "1" and $tomorrow <= "4" )
{
$hourstomorrow = $MRhours ;
$tomorrowdaytype = "weekday" ;
}
if ( $tomorrow == "5" )
{
$hourstomorrow = $Fhours ;
$tomorrowdaytype = "weekday" ;
}
if ( $tomorrow == "6" )
{
$hourstomorrow = $Shours ;
$tomorrowdaytype = "weekend" ;
}
if ( $tomorrow == "7" )
{
$hourstomorrow = $SUhours ;
$tomorrowdaytype = "weekend" ;
}
if ( $tomorrow == "8" )
{
$tomorrow = "1" ;
$tomorrowdaytype = "weekday" ;
}
}
if ( $tomorrowdaytype = "weekday" )
{
$opentime = $hourstomorrow["beginam"] ;
}
if ( $tomorrowdaytype = "weekend" )
{
$opentime = $hourstomorrow["beginbrunch"] ;
}
//set message
if ( ( $status == "not yet open" && $daytype == "weekend" ) or ( $status == "closed" && $tomorrowdaytype == "weekend" ) )
{
$message = "Mims is closed right now, but we open at 9:00 AM for brunch, and we hope you'll join us then!" ;
}
if ( ( $status == "not yet open" && $daytype == "weekday" ) or ( $status == "closed" && $tomorrowdaytype == "weekday" ) )
{
$message = "Mims is closed right now, but we open at 7:00 AM, and we hope you'll join us then!" ;
}
if ( $status == "brunch" )
{
$message = "Mims is currently serving brunch. Come on in!" ;
}
if ( $status == "am" )
{
$message = "Mims is currently serving breakfast. Come on in!" ;
}
if ( $status == "md" )
{
$message = "Mims is currently serving lunch. Come on in!" ;
}
if ( $status == "pm" )
{
$message = "Mims is currently serving dinner. Give us a call and reserve a table!" ;
}
if ( $status == "soups" )
{
$message = "Mims is currently serving just soups and cocktails while the kitchen prepares for our full evening service, which begins at 5:00. Call us up and reserve a table!" ;
}
if ( $status == "cocktails" )
{
$message = "Mims is currently serving only cocktails while the kitchen prepares for our full evening service, which begins at 5:00. Call us up and reserve a table!" ;
}
//output
echo strftime("Here in Portland, today is %A %e %B, and the time is %I:%M %p. <br>") ;
echo("$message<br>") ;
?>
[/php]
|
|
Hi. Nice to see you. You look lovely.
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2004
Location: Toronto, ON
Status:
Offline
|
|
Originally posted by AWShuttleworth:
Well, after spending a lot of time at the PHP website, I've finally written some code that seems to do what I need it to do. I haven't cleaned it up or merged it with my CSS layout, yet, but if you're interested, this is what I've come up with.
A quick glance doesn't reveal anything majorly wrong aside from some formatting issues that don't really matter for you. I would suggest that your 'which set of hours', 'closed status', and 'set message' sections be converted to switch statments. It will give you a bit of a performance increase since you won't be doing multiple comparisons.
|
|
|
| |
|
|
|
 |
|
 |
|
Registered User
Join Date: Sep 2002
Location: New York City
Status:
Offline
|
|
Another way to improve the speed...
if a string does not contain any variables, then use single-quotes
my understanding of strings is that the double-quote is analyzed more thoroughly so that any "$__" variables are replaced by their value; a single-quote is just a straight forward string
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|