php strtotime('today') 返回不正确的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9957616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:58:54  来源:igfitidea点击:

strtotime('today') returning incorrect time?

phpunixtimestrtotimetimezone-offset

提问by Johnathan Au

I am trying to create a select list starting from the current date of the user. I want it so that it is set to midnight in unix timestamp format.

我正在尝试从用户的当前日期开始创建一个选择列表。我希望它以 unix 时间戳格式设置为午夜。

This is all I'm doing:

这就是我正在做的一切:

$today = strtotime('today');
echo $today;

This is my result:

这是我的结果:

1333144800

which is: Fri, 30 Mar 2012 22:00:00 GMT according to Epoch Converter (incorrect by a couple hours.)

即:根据 Epoch Converter,格林威治标准时间 2012 年 3 月 30 日星期五 22:00:00(错误几个小时。)

回答by Niko

If you want strtotime() to return a timestamp relative to UTC (00:00:00 UTC instead of e.g. 00:00:00 UTC+2, if your system is set to a timezone with an offset of 2 hours against UTC/GMT), you need to specify that:

如果您希望 strtotime() 返回相对于 UTC 的时间戳(00:00:00 UTC 而不是 00:00:00 UTC+2,如果您的系统设置为相对于 UTC/GMT 偏移 2 小时的时区),您需要指定:

$today = strtotime('today UTC');

回答by Xfile

GMT (+0) time

GMT (+0) 时间

echo $today = strtotime('today GMT');
echo "<br>" . $today = date("d-m-Y H:i:s", $today);

We expect that your server runs at GMT - that is the best (for maneuvering with time displays later). If not, you MUST adjust php.ini set this "date.timezone = GMT".

我们希望您的服务器在格林威治标准时间运行 - 这是最好的(用于稍后显示时间)。如果没有,你必须调整 php.ini 设置这个“date.timezone = GMT”。

When you get that done, you will see 00:00 with my codes.

完成后,您将看到带有我的代码的 00:00。

Then, you must develop function (ie DisplayDate()) in your script to display dates of your website correctly if

然后,您必须在脚本中开发函数(即 DisplayDate())以正确显示您网站的日期,如果

  • youre not in GMT area
  • or/and if you want for your users to see times in their timezone with timezone selection for example.
  • 你不在格林威治标准时间区域
  • 或/和如果您希望您的用户通过时区选择查看时区中的时间。

DisplayDate() should include support for daylight changes also (0, or +1 hour / summer and winter time).

DisplayDate() 还应包括对日光变化的支持(0 或 +1 小时/夏令时和冬令时)。

回答by Goldentoa11

strtotime( $time )

is designed to return a unix timetamp, meaning, it will return the number of seconds since jan 1, 1970. http://www.php.net/manual/en/function.strtotime.php

旨在返回 unix 时间戳,这意味着它将返回自 1970 年 1 月 1 日以来的秒数。http://www.php.net/manual/en/function.strtotime.php

To get around this, use something like:

要解决此问题,请使用以下内容:

$today = date("d/m/Y H:i:s", strtotime('today'));
echo $today;

回答by hohner

You might have to specifically set the time as well as the day:

您可能需要专门设置时间和日期:

$today_midnight = strtotime('today UTC 00:00');

回答by PachinSV

You should check the timezone configuration in your php.ini file. In my case (I live in El Salvador) I had to change it like this:

您应该检查 php.ini 文件中的时区配置。就我而言(我住在萨尔瓦多)我不得不像这样改变它:

date.timezone = America/El_Salvador