Jump to content

Need local time script that shows am and pm


Bluemoon

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.