Bluemoon Posted August 27, 2009 Share Posted August 27, 2009 I'd like something like this on my page: Office Hours: 8 am - 5 pm EST The time is now: 8:30 pm The office is now closed. The bolded part is what I need to insert via PHP. So far, I have a very basic local time script: <? putenv("TZ=US/Eastern"); echo "The time is now: ". date("h:i:s")."\n"; ?> However, this doesn't automatically update the time without refreshing the page. How can I modify this script to: - include "am" and "pm" - update automatically without refreshing page - say the office is now closed or open depending on the time Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/172201-need-local-time-script-that-shows-am-and-pm/ Share on other sites More sharing options...
quasiman Posted August 27, 2009 Share Posted August 27, 2009 I'm curious about the "now closed" part. Are you trying to do some math that compares the current time with closing time? If so, you'll have to do it again when you open again in the morning....and, then of course, you have to take into account the weekends, right? Or are you open all week? Quote Link to comment https://forums.phpfreaks.com/topic/172201-need-local-time-script-that-shows-am-and-pm/#findComment-907955 Share on other sites More sharing options...
ignace Posted August 27, 2009 Share Posted August 27, 2009 ....and, then of course, you have to take into account the weekends, right? Or are you open all week? And then we aren't yet talking about variable hours or lunch time? Quote Link to comment https://forums.phpfreaks.com/topic/172201-need-local-time-script-that-shows-am-and-pm/#findComment-907958 Share on other sites More sharing options...
Bill.Oakley Posted August 27, 2009 Share Posted August 27, 2009 I would think you would mainly be interested in showing the current status when a user logs onto the page and this could be done simply enough with something like: if (date("D")=="Sat"||date("D")=="Sun"){echo"Closed";} else {$smtime=date("H"); if ($smtime<8||$smtime>=17) {Closed";} else if ($smtime<=8||$smtime<17){echo"Open";} else {echo"Open";} } you could also I suppose add a javascript to compare client local time to server time and have it call the display every hour to check. Quote Link to comment https://forums.phpfreaks.com/topic/172201-need-local-time-script-that-shows-am-and-pm/#findComment-907974 Share on other sites More sharing options...
gamesmstr Posted August 27, 2009 Share Posted August 27, 2009 To automatically update the time without a refresh, I would suggest googling javascript clocks. You are going to need a client side language like javascript to get the updating clock. Quote Link to comment https://forums.phpfreaks.com/topic/172201-need-local-time-script-that-shows-am-and-pm/#findComment-907976 Share on other sites More sharing options...
ignace Posted August 28, 2009 Share Posted August 28, 2009 I would think you would mainly be interested in showing the current status when a user logs onto the page and this could be done simply enough with something like: if (date("D")=="Sat"||date("D")=="Sun"){echo"Closed";} else {$smtime=date("H"); if ($smtime<8||$smtime>=17) {Closed";} else if ($smtime<=8||$smtime<17){echo"Open";} else {echo"Open";} } else if ($smtime<=8||$smtime<17){echo"Open";} Oh so you are only closed between 17 and 23:59 And when would: else {echo"Open";} ever execute? Quote Link to comment https://forums.phpfreaks.com/topic/172201-need-local-time-script-that-shows-am-and-pm/#findComment-908185 Share on other sites More sharing options...
Bluemoon Posted August 29, 2009 Author Share Posted August 29, 2009 Thanks for the replies! This is a great forum - very helpful. I decided to go with a javascript script to generate my local time in real time. But I can use this PHP script to generate the " now open" script. (And, no, I am not worried about showing lunch breaks - just in showing that the office is open between 8-5, M-F.) Quote Link to comment https://forums.phpfreaks.com/topic/172201-need-local-time-script-that-shows-am-and-pm/#findComment-908781 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.