How to convert datetime to GMT in php -


alright, i'm not sure if im converting user input time gmt properly. having users across several timezones entering "events" , have able see "how long untill" or "how long since" current time();

this how planning convert time input. start 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 planning take value of gmdate('y-m-d h:i:s',strtotime('07/21/2011 01:30 am')); take time() , display "how long until event" users. seems there 10 hours added onto result, if if scheduling event 30 min 10 hours 30 min now. so, im thinking im not converting local time correctly or something.

what missing? maybe dont understand gmt. how can make sure times involved gmt times universal users on website?

other info if helps: server timezone america/los_angeles

edit: after everyones suggestions i've tried setting @ top of php code:

date_default_timezone_set("gmt");

and tried using date('y-m-d h:i:s') comparison figure out diff, saying "3 hours ago" rather 10 hours now. definately changed things.

but still not correct.

i've confirmed date('y-m-d h:i:s') returning correct , current gmt. thats good.

but user input date off. how converting incorrectly?


edit again(including test results after salman a's suggestions):

2:55am - current local time est

date('y-m-d h:i:s') shows 2011-07-21 06:56:43 - correct

3:00am est time in future submitted 07/21/2011 03:00 am

here's how time "convert it" , submit db:

$time = $_post['time'];  //there im assuming turns est time gmt equivalent. $the_date = strtotime($time . ' gmt');  $utctime = gmdate('y-m-d h:i:s',$the_date); 

i'm expecting function tell me event 5 minutes now, hours off.

just make sure user submitted time converted gmt display $utctime , shows 2011-07-21 03:00:00 - not 08:00 or 07:00 (which think 1 of gmt equivalent)

so how convert it?

so, im seeing strtotime($time . ' gmt'); doesn't seem applying gmt local time supply. on side note: somone suggested have date_default_timezone_set("gmt"); in code, have @ top. should remove it? noticed if remove gmt incorrect. thats why left it.

if need calculate difference between 2 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 $time has same timezone used time() function i.e. server's timezone.

if timezones differ, must normalize them in order subtraction work expected. example if you're storing gmt date/time in database 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 code:

$time = $_post['time']; 

if users various parts of world, should either:

  • ask them enter date/time in gmt
  • ask them enter timezone date entered

you can later convert date on server side , store 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 , 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                                           // store in database                                           // same 2011-07-21 17:00:00 minus 7 hours 

without timezone information calculations unreliable.

edit #2

actually... 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 // store in 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 gmt: ' + d.toutcstring());     }     document.getelementbyid("offset").value = (new date()).gettimezoneoffset() / 60; </script> 

demo here


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -