Jump to content

PHP clock to show all visitors current time in a specific country


Recommended Posts

Hi,

 

I'm developing a website based in Kenya.  I would like a clock to be displayed on the site which always shows the current time in Kenya.

 

I know 0 about PHP but it was suggested to me that perhaps php could provide this effect by grabbing the web hosts time and then adding/subtracting hours to/from that time to reflect the current time in Kenya.

 

I'm happy to be pointed in the direction of a tutorial or two which fits this description, but I'm hoping for something fairly specific here as I don't have a lot of time for experiments with chopping and changing code (I'm already doing enough of that with css).  Thanks to anyone with the time to assist.

Kenya is GMT +3 correct? Just did a quick Google...

 

The way it works is, instead of using date you use gmdate.

 

$current_date = gmdate("jS F Y, h:ia", time() + (3 * 60 * 60));

echo $current_date;

 

Okay, so the first part of gmdate() is the format of the date output, you can check that out on the date page, and customize it to how you want.

 

Now, we want to get it from the current time, plus your timezone offset (3), multiply that by 60 (minutes) and then multiply that by 60 (seconds) again. This will give us the correct offset from GMT.

 

Good luck.

Or using pretty classes:

 

$tz = new DateTimeZone('Etc/GMT-3');
$dt = new DateTime('now', $tz);

echo $dt->format('r');
//Thu, 18 Feb 2010 15:33:39 +0300

 

(Edit: typoed "Our"... grr)

Hi Sal and PF,

 

Thanks for the response.  I'm not sure what Kenya is compared to the server I want to use to get the original time, cause I don't yet know what time zone the server is.  I was planning on just doing a bit of trial and error with that once I had the basic code worked out.

 

I'm surprised the code looks so short.  Thanks for posting it, but I'm afraid I don't quite know how to implement it.  Is it meant to work straight away when I upload it onto the web?  ( I know I could test this out by just uploading it and seeing what happens, but I actually don't have time for any webwork today and I'm rushing so I thought I could lean a bit more on your patience by asking directly).

 

What I mean is, the way the code is written now, will it understand to automatically grab the time from the webhost I am using or do I need to ALSO make some kind of arrangements with the webhost to allow this code to grab the time?

 

You may not be able to answer a question like that without knowing the specifics of the webhost set up, but just a general idea of how it is meant to work will be helpful.  The way you've expressed it now, it seems that I only need to paste the code onto the page (apparently with some kind of special opening and closing PHP tags as well, which I can look up later); is that right?

 

Thanks again.

 

(btw, I'm surprised how busy this forum is.  I don't know how you guys keep up with so much information. I only posted yesterday and there was something like 50 new threads on top of mine!  Anyway, keep up the good work).

 

Don't worry about the server time and all that, because it will have no importance. Take a look at gmdate in the manual, it's quite different from the standard date function because it returns the Greenwich Mean Time.

The good thing about that is, that it's simply GMT, so from there we can plus or minus the amount of hours that we want. In the case of Kenya,  +3 hours.

 

Salathe code will work the same way. I haven't looked to far into the class, but I'm assuming because the code is requested a DateTimeZone of +3 that the time it formats will be in Kenya's time, and the time of the server will have nothing to do with it.

 

Only if you were to use date would it provide you with the time of the server.

 

Hope this helps you out a little bit more.

Salathe code will work the same way. I haven't looked to far into the class, but I'm assuming because the code is requested a DateTimeZone of +3 that the time it formats will be in Kenya's time, and the time of the server will have nothing to do with it.

 

That's right except that the time is taken from the server (you maybe meant "timezone of the server") and calculated to Kenya's time. If the server is a couple of seconds (or god forbid, minutes!) deviant from actual time, that difference will be reflected. 

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.