如何在 PHP 中将日期时间转换为 GMT

发布于 2024-11-25 09:23:01 字数 1827 浏览 1 评论 0原文

好吧,所以我不确定我是否正确地将用户输入时间转换为 GMT。我将让跨多个时区的用户输入“事件”,他们必须能够看到“多久之前”或“多久之后”当前 time();

这就是我的方式计划转换他们输入的时间。它的开头类似于 07/21/2011 01:30 am 然后,

echo gmdate('Y-m-d 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 小时 30 分钟。所以,我想我没有正确转换当地时间或其他什么。

我缺少什么?也许我只是没有正确理解 GMT。我如何确保所有涉及的时间均为 GMT,以便所有时间对于网站上的所有用户都是通用的?

其他信息(如果有帮助): 服务器时区是 America/Los_Angeles

编辑: 在大家的建议之后,我尝试将其设置在我的 php 代码的顶部:

date_default_timezone_set("GMT");

并且我尝试使用 date('Ymd H:i:s')< /code> 进行比较以找出差异,但它说的是“3 小时前”而不是从现在起 10 小时。所以这肯定改变了事情。

但还是不正确。

我已确认 date('Ymd H:i:s') 返回正确且当前的 GMT。那很好。

但用户输入的日期已关闭。我如何错误地转换它?


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

凌晨 2:55 - 我当前的当地时间 EST

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

3:00am EST 是我提交的未来时间 07/21/2011 03:00 am

以下是我如何获取时间“转换它”并将其提交到我的数据库:

$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);

我希望我的函数告诉我该事件是从现在开始 5 分钟,但已经关闭了几个小时。

只是为了确保用户提交的时间实际上已转换为 GMT,我显示 $utctime 并显示为 2011-07-21 03:00:00 - 这不是08:00 或 07:00(我认为其中之一是 GMT 等效时间)

那么我该如何转换它呢?

所以,我看到的是 strtotime($time . ' GMT'); 似乎没有将 GMT 应用于我提供的当地时间。附带说明:有人建议我在代码中添加 date_default_timezone_set("GMT"); ,所以我把它放在顶部。我应该删除它吗?但我注意到如果我删除它,GMT 就不正确。这就是我离开它的原因。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

允世 2024-12-02 09:23:01

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

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

上面的示例假设 $timetime() 函数使用的时区相同,即服务器的时区。

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

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

编辑 1

关于此代码:

$time = $_POST['time'];

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

  • 询问他们输入 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)
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

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

编辑 #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
// THIS is what you store in your database

编辑 #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>

此处演示

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

攒眉千度 2024-12-02 09:23:01

一个好主意是明确设置脚本的时区。例如:

date_default_timezone_set('Europe/London');

这将使所有日期函数返回 GMT 日期,并且我相信也考虑了 BST。

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.

清风夜微凉 2024-12-02 09:23:01

您好,我住在波兰,我们有欧洲中部时间。
要将 CET 转换为 GMT,我使用函数:

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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文