Jump to content

Recommended Posts

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.

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?

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.

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.