简体   繁体   English

PHP的:设置日期的时区

[英]PHP : Setting timezone for date

i'm saving login time for php . 我节省了php的登录时间。 this is what i use to save value against every login 这就是我用来为每次登录节省价值的方式

$update= mysqli_query($connection, "UPDATE employees set loggedin= now() WHERE email = '$email'");       

when i get values from 当我从中获得价值时

  $logintime_query=  mysqli_fetc h_assoc($logintime);
  $loggedin_time_converted= explode(" ",$logintime_query) ;       

  $converted_time= $loggedin_time_converted['0'];
  $converted_date= $loggedin_time_converted['1'];

now i've to compare this date with today's date , to get today's date i am using this 现在我必须将此日期与今天的日期进行比较,以获取我正在使用的今天的日期

$today=  $_SERVER['REQUEST_TIME'];
echo(date("Y-m-d",$today));

now problem is that $_SERVER['REQUEST_TIME] is giving me this date 现在的问题是$_SERVER['REQUEST_TIME]给我这个日期

2015-12-21

and when i enter date using NOW() and retrieve it from DB this is what i get 当我使用NOW()输入日期并从数据库中检索日期时,这就是我得到的

2015-12-20

is it time zone issue or what ? 是时区问题还是什么? my time zone is utc+4 (Dubai) so i tried to set it like this 我的时区是utc + 4(迪拜),所以我尝试将其设置为

date_default_timezone_set('Asia/Dubai');

but this does not work some one please help me to figure this out 但这对某些人无效,请帮助我解决这个问题

Timezones are complicated and messy. 时区既复杂又混乱。 For example, here your problem is not only concerning the PHP timezone setting, but your MySQL timezone as well since you're storing timestamps there and trying to use them again in PHP. 例如,在这里,您的问题不仅与PHP时区设置有关,而且与MySQL时区有关,因为您要在此存储时间戳并尝试在PHP中再次使用它们。

Enter Unix Timestamps for Transport 输入Unix时间戳进行传输

The better and more effective way to transport all these timestamps around so that you don't have to worry about timezone conversions when you go from MySQL to PHP or PHP to MySQL or PHP to Javascript or Javascript to PHP, is to just use Unix timestamps everywhere the time needs to be transported. 更好地,更有效地传输所有这些时间戳,以便从MySQL到PHP或PHP到MySQL或PHP到Javascript或Javascript到PHP时,不必担心时区转换,就是使用Unix时间戳。到处都需要时间。

$logintime =  mysqli_query("SELECT UNIX_TIMESTAMP(loggedin) as loggedin FROM employees WHERE email = 'josh@foobar.baz'");

Now when you want to compare this in PHP you simply do ... 现在,当您想在PHP中进行比较时,您只需...

if ($logintime['loggedin'] > time()) {
    /* ... do stuff ... */
}

To go from PHP back to MySQL is just as simple... 从PHP返回MySQL很简单...

$t = time();
$logintime =  mysqli_query("UPDATE employees SET loggedin = FROM_UNIXTIMESTAMP($t) WHERE email = 'josh@foobar.baz'");

This is so much more convenient than having to worry about whether or not you've correctly set timezones in various places and hope that you haven't made a simple mistake somewhere along the way that will mess with your results later. 这比担心您是否已在各个地方正确设置时区并希望您在此期间未在某个地方犯一个简单的错误而导致以后的结果混乱要方便得多。

I suggest you to use time() function only. 我建议您仅使用time()函数。 See here . 这里

If you set date_default_timezone_set('Asia/Dubai'); 如果您设置了date_default_timezone_set('Asia/Dubai'); before using time() It should work. 在使用time()之前,它应该工作。

not tested. 未经测试。

It should be a timezone issue, yes. 是的,应该是时区问题。 Whats the timezone of your mysql server? 您的mysql服务器的时区是多少?

You can globally change it with 您可以使用

default_time_zone='+04:00'

in the mysqld section of your mysqld. 在mysqld的mysqld部分中。

Or with the following sql statement: 或使用以下sql语句:

SET @@global.time_zone='+04:00';

Also be aware that $_SERVER['REQUEST_TIME] is the time the webserver started to process your request. 另外请注意,$ _ SERVER ['REQUEST_TIME]是网络服务器开始处理您的请求的时间。 Using php's date() function should be more accurate. 使用php的date()函数应该更准确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM