如何将php中的datetime转换为GMT [英] How to convert datetime to GMT in php

查看:99
本文介绍了如何将php中的datetime转换为GMT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我不知道是否将用户输入时间转换为GMT正确。我将使用户跨越多个时区进入事件,他们必须能够看到多久直到或多长时间当前 time();



这是我打算如何转换他们输入的时间。它将像 07/21/2011 01:30 am开始然后,

  echo gmdate('Ymd H:i:s',strtotime('07 / 21/2011 01:30 am')); 

给我 2011-07-21 08:30:00



所以我打算采取 gmdate('Ymd H:i:s',strtotime('07 / 21/2011 01:30 am')); 然后采取 time()并显示直到此事件多长时间给用户。但是,似乎总是有10个小时添加到结果中,所以如果我从现在起30分钟安排一个事件,那么从现在开始就会说10个小时。所以,我认为不能正确地转换当地时间或某事。



我缺少什么?也许我根本不懂格林尼治标准时间如何确保所有涉及的时间都是GMT,所有时间都是通用的网站上的所有用户?



其他信息,如果它有帮助:
服务器时区为美国/ Los_Angeles



编辑:
在每个人的建议之后,我尝试将其设置为我的PHP代码的顶部:



date_default_timezone_set(GMT);



,我尝试使用 date('Ymd H:i:s')进行比较来找出差异,但它的3小时前,而不是从现在开始的10个小时。所以这确实改变了事情。



但仍然不正确。



我已经确认了 date('Ymd H :i:s')正在返回正确的和当前的GMT。所以很好。



但用户输入的日期已关闭。如何正确转换?






再次编辑(包括Salman A的建议后的一些测试结果):



2:55 am - 我目前本地时间EST



date('Ymd H:i:s ')显示为 2011-07-21 06:56:43 - 这是正确的


$ b $美国东部时间上午3:00美国东部时间今天的时间是 07/21/2011 03:00 am



这是我如何获得时间转换并将其提交到我的数据库:

  $ time = $ _POST ['时间']; 

//其中im假设将我的EST时间转换为GMT等价物。
$ the_date = strtotime($ time。'GMT');

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

我期待我的功能告诉我事件距离现在5分钟,但是它的小时



只是为了确保用户提交的时间实际转换为GMT我显示 $ utctime 并显示作为 2011-07-21 03:00:00 - 这不是08:00或07:00(我认为其中一个将是GMT等价物) p>

那么如何转换?



所以,看到的是 strtotime($时间。GMT); 似乎没有将GMT应用于当地提供的时间。在旁注:somone建议我在我的代码中有 date_default_timezone_set(GMT); ,所以我在顶部。我应该删除它吗但我注意到,如果我删除它,GMT是不正确的。所以这就是为什么我离开它。

解决方案

如果你只需要计算两个时间值之间的差异:

 <?php 
$ time = '07 / 21/2011 11:30 am'
$ timeleft = strtotime($ time) - time();
//目标时间....:2011-07-21 11:30:00
//当前时间...:2011-07-21 11:13:45
/ /差别.....:975秒(16分钟,15秒)

上面的例子假设 $ time 具有与 time()函数所使用的时区相同的时区,即服务器的时区。



如果时区不同,您必须对它们进行规范化,以使减法按预期工作。因此,例如,如果您在数据库中存储 GMT 日期/时间,则上述示例将变为:

 <?php 
$ time = '07 / 21/2011 06:30 am'
$ timeleft = strtotime($ time。'GMT') - time();
//目标时间............:2011-07-21 06:30:00 GMT
//转换本地时间...:2011-07-21 11:30:00 PKT
//当前时间...........:2011-07-21 11:34:48 PKT
//差异...... .......:-288秒(减4分48秒)



编辑1



关于此代码:

  $ time = $ _POST ['时间']; 

如果您的用户来自世界各地,您应该:




  • 要求他们以GMT格式输入日期/时间

  • 要求他们输入输入日期的时区。 >


您可以稍后在服务器端转换日期并将其存储在数据库中:

 <?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)
列表(
$ temp_hh,
$ temp_mm
)= explode(':',date ')); //返回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
//这是你在数据库中存储的
//与2011-07-21 17:00:00减去7小时

没有时区信息,您的计算将不可靠。



编辑#2



其实更简单:

 <?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
//这是您存储在数据库中的



编辑#3



 < input type =textname =timeid =timevalue =07/21/2011 17:00> 
< input type =textname =offsetid =offset>
< script type =text / javascript>
document.getElementById(time)。onchange = function(){
var d = new Date(this.value);
alert('输入日期:'+ d +'\日期到GMT:'+ d.toUTCString());
}
document.getElementById(offset)。value =(new Date())。getTimezoneOffset()/ 60;
< / script>

Demo here


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.

解决方案

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

这篇关于如何将php中的datetime转换为GMT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆