0

Alright, so i'm not sure if im converting user input time to GMT properly. I will be having users across several timezones entering "events" and they will have to be able to see "how long untill" or "how long since" the current time();

This is how I was planning to convert the time they input. It will start as something like 07/21/2011 01:30 am Then,

echo gmdate('Y-m-d H:i:s',strtotime('07/21/2011 01:30 am'));

gives me 2011-07-21 08:30:00

So I was planning to take the value of gmdate('Y-m-d H:i:s',strtotime('07/21/2011 01:30 am')); then take time() and display "how long until this event" to users. But it seems like there is always 10 hours added onto the result, so if if i was scheduling an event 30 min from now it would say 10 hours 30 min from now. So, im thinking im not converting the local time correctly or something.

What am I missing? Maybe I just dont properly understand GMT. How can I make sure all the times involved are GMT so all times are universal to all the users on the website?

Other info if it helps: The server timezone is America/Los_Angeles

EDIT: After everyones suggestions i've tried setting this at the top of my php code:

date_default_timezone_set("GMT");

and I tried using date('Y-m-d H:i:s') to do the comparison to figure out the diff, but its saying "3 hours ago" rather than the 10 hours from now. So this definately changed things.

But still not correct.

I've confirmed date('Y-m-d H:i:s') is returning the correct and current GMT. So thats good.

But the user input date is off. How am I converting it incorrectly?


EDIT AGAIN(including some test results after Salman A's suggestions):

2:55am - my current local time EST

date('Y-m-d H:i:s') shows up as 2011-07-21 06:56:43 - which is correct

3:00am EST is the time in the future I submitted as 07/21/2011 03:00 am

Here's how I get the time "convert it" and submit it to my DB:

$time = $_POST['time'];

//there is where im assuming it turns my EST time to the GMT equivalent.
$the_date = strtotime($time . ' GMT');

$utctime = gmdate('Y-m-d H:i:s',$the_date);

I'm expecting my function to tell me the event is 5 minutes from now, but its hours off.

just to make sure the user submitted time was actually converted to GMT i display $utctime and it shows up as 2011-07-21 03:00:00 - which is not 08:00 or 07:00 (which i think one of those would be the GMT equivalent)

So how do I convert it?

So, what im seeing is strtotime($time . ' GMT'); doesn't seem to be applying the GMT to the local time I supply. On a side note: somone suggested I have date_default_timezone_set("GMT"); in my code, so i have it at the top. Should I remove it? but i noticed if i remove it the GMT is incorrect. So thats why I left it.

brybam
  • 5,009
  • 12
  • 51
  • 93

3 Answers3

2

If you simply need to calculate the difference between two time values:

<?php
$time = '07/21/2011 11:30 am';
$timeleft = strtotime($time) - time();
// target time....: 2011-07-21 11:30:00 
// current time...: 2011-07-21 11:13:45
// difference.....: 975 seconds (16 min, 15 seconds)

The above example assumes that $time has same timezone as that used by the time() function i.e. the server's timezone.

If the timezones differ, you must normalize them in order for subtraction to work as expected. So for example if you're storing GMT date/time in your database then the above example becomes:

<?php
$time = '07/21/2011 06:30 am';
$timeleft = strtotime($time . ' GMT') - time();
// target time............: 2011-07-21 06:30:00 GMT
// converted local time...: 2011-07-21 11:30:00 PKT
// current time...........: 2011-07-21 11:34:48 PKT
// difference.............: -288 seconds (minus 4 minutes, 48 seconds)

Edit 1

Regarding this code:

$time = $_POST['time'];

If your users are from various parts of the world, you should either:

  • ask them to enter the date/time in GMT
  • ask them to enter a timezone for the date entered

You can later convert the date on server side and store it in database:

<?php
$source_time     = '2011-07-21 17:00';
$source_offset   = '-0700'; // PDT
$local_timestamp = strtotime($source_time . ' ' . $source_offset); // 2011-07-22 05:00 PKT (SERVER TIME)
list(
    $temp_hh,
    $temp_mm
)                = explode(':', date('P')); // returns difference between SERVER TIME and GMT
$local_offset    = $temp_hh * 3600 + $temp_mm * 60;
$gmt_timestamp   = $local_timestamp + $local_offset;
echo date("Y-m-d H:i:s", $gmt_timestamp); // 2011-07-21 10:00:00
                                          // THIS is what you store in your database
                                          // Same as 2011-07-21 17:00:00 minus 7 hours

Without the timezone information your calculations will be unreliable.

Edit #2

Actually... it is much simpler:

<?php
$source_time   = '2011-07-21 17:00';
$source_offset = -7.0; // -0700
echo date("Y-m-d H:i:s", strtotime($source_time) + $source_offset * 3600);
// 2011-07-21 10:00:00
// THIS is what you store in your database

Edit #3

<input type="text" name="time" id="time" value="07/21/2011 17:00">
<input type="text" name="offset" id="offset">
<script type="text/javascript">
    document.getElementById("time").onchange = function(){
    var d = new Date(this.value);
    alert('Date entered: ' + d + '\nDate to GMT: ' + d.toUTCString());
    }
    document.getElementById("offset").value = (new Date()).getTimezoneOffset() / 60;
</script>

Demo here

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • "you must normalize them in order for subtraction to work as expected" Thats where my confusion lies. I already have a function worked out to display the time difference. But it's off. So, im assuming im not property converting the user input times to a normalized GMT. How can I do that? I gave an example up top: `echo gmdate('Y-m-d H:i:s',strtotime('07/21/2011 01:30 am'));` this is what im doing with user input times (which are in their local timezones) how can I normalize them? – brybam Jul 21 '11 at 06:26
  • @brybam: I just re-wrote my answer. See if that makes sense. In summary, you need both times to have same timezone before you subtract them. strtotime is smart enough to do all math for you... just add the timezone suffix. `+0500`, `PST`, `GMT` etc will work. – Salman A Jul 21 '11 at 06:42
  • It doesn't seem to be changing the local time to GMT. I updated the question above and explained a test I did – brybam Jul 21 '11 at 07:03
  • Ummm... actually it is converting GMT time to local time... `strtotime()` converts the time to local time if timezone is specified. – Salman A Jul 21 '11 at 07:59
  • I need it to convert the local time to GMT time. What im saying above is, I gave it 3am EST, I ran the functions on it I show above, and it returns 2011-07-21 03:00:00 EST rather than making it into whatever the GMT is... i updated the code above to show how i see that its not converting my local time. – brybam Jul 21 '11 at 08:13
  • Is it possible that on the post, I get the users timezone with php and convert it to GMT then store the GMT on the DB? – brybam Jul 21 '11 at 08:37
  • @brybam: you're welcome to search on stackoverflow. Its worth posting as a separate question. – Salman A Jul 21 '11 at 08:41
  • But it's this question... `How to convert datetime to GMT` am I missing something? My last comment was directly related to the original question and would be an essential part of how to convert it. – brybam Jul 21 '11 at 08:45
  • To get the *user's* timezone via PHP is a separate question... there are javascript solutions. – Salman A Jul 21 '11 at 08:50
  • The javascript solutions wouldnt work because this is all being done serverside on the POST and submitted to the DB. So it would have to be done in php. – brybam Jul 21 '11 at 08:57
  • Actually scratch that, it looks like there is a tricky javascript thing where you can store the timezone in a cookie ill have to look into. Anyway, this entire time i was confused because I thought thats what strtotime was doing. – brybam Jul 21 '11 at 09:02
1

Hello I am living in Poland and we have a CET time. To convert CET to GMT I am using function:

function convertCETtoGMT($timeCET)
    {
        date_default_timezone_set('EUROPE/London');
        $time = $timeCET." CET";
        $timeGMT = date('Y-m-d H:i:s', strtotime($time));
        date_default_timezone_set('EUROPE/Warsaw'); //set back to CET
        return $timeGMT;
    }
Tikky
  • 1,253
  • 2
  • 17
  • 36
1

A good idea is to explicitly set the timezone for your scripts. For example:

date_default_timezone_set('Europe/London');

This will make all date functions returns dates in GMT, and I believe accounting for BST too.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • Thanks for the tip. But I already have date_default_timezone_set("GMT"); set. My issue is just with the user input date/times I dont know how to convert it. If you look towards the bottom of my question I showed a test and the results. – brybam Jul 21 '11 at 08:21
  • Maybe check out this article then: http://blog.boxedice.com/2009/03/21/handling-timezone-conversion-with-php-datetime/ – Martin Bean Jul 21 '11 at 09:23