Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/MySQL中的时区帮助_Php_Mysql_Datetime_Timezone_Timestamp - Fatal编程技术网

PHP/MySQL中的时区帮助

PHP/MySQL中的时区帮助,php,mysql,datetime,timezone,timestamp,Php,Mysql,Datetime,Timezone,Timestamp,我已经打了好几个星期的时区之战了,每个人都认为他们知道解决方案,然后他们得到了一百万张选票,但事实是我完全被卡住了,没有一个关于PHP/MySQL中时区支持的答案100%对我有效,因此,我在这里再次提出这个问题,尽可能详细地介绍代码示例和所有内容 如果你是一个PHP/MySQL专家,或者甚至是一个非常幸运的乞丐,并且已经让PHP/MySQL/用户提交的时区工作,那么请提供帮助 下面是我到目前为止的情况,请阅读评论,看看我有什么问题。基本上,我可以在用户时区中显示日期和时间,如果它是以类似于126

我已经打了好几个星期的时区之战了,每个人都认为他们知道解决方案,然后他们得到了一百万张选票,但事实是我完全被卡住了,没有一个关于PHP/MySQL中时区支持的答案100%对我有效,因此,我在这里再次提出这个问题,尽可能详细地介绍代码示例和所有内容

如果你是一个PHP/MySQL专家,或者甚至是一个非常幸运的乞丐,并且已经让PHP/MySQL/用户提交的时区工作,那么请提供帮助

下面是我到目前为止的情况,请阅读评论,看看我有什么问题。基本上,我可以在用户时区中显示日期和时间,如果它是以类似于1262819848的时间戳的形式显示,但是MySQL的较新版本会返回类似于2010-01-06 23:17:28的时间戳

如果我能通过2010-01-06 23:17:28进入这个日期M:d:y h:I:s A,$time_戳,那么事情会很顺利,但它需要一个时间戳。我能做到这一点的唯一方法是在MySQL中的整数字段中存储一个真实的时间戳,或者获取值2010-01-06 23:17:28,然后将其转换为时间戳,当我可以将其存储在INT字段中时,这似乎会带来很大的开销

如果你能帮我使用datetime函数,我将非常感谢你的帮助,我已经在这个问题上纠缠了几年了。我打算放弃,像所有人一样使用整数字段,因为某些原因,所以说它不好

<?PHP
/////////////////////////////////////////////////////////
//   Get a MySQL time / date and show in user timezone //
////////////////////////////////////////////////////////

//set the user's time zone for this page
//this assumes the user has already chosen a timezone and we are getting it from a PHP session value now
// example value is "America/New_York" which is -5
date_default_timezone_set($_SESSION['time_zone']);

// get a MySQL result with a time/date value in it
$sql = 'SELECT user_id,date from online_users WHERE user_id = 1';
$result = executeQuery($sql);

// set our date/time value from MySQL into a variable
if ($line_online = mysql_fetch_assoc($result)){
    $time_stamp = $line_online['date'];
}

// format our date/time value TIMESTAMP any way we like now!
// MySQL result MUST be in this format '1262291376'
$date_format = date("m:d:y h:i:s A",$time_stamp); // formats timestamp in mm:dd:yy 
echo $date_format;


/////////////////////////////////////////////////////////
//   Add a time / date to MySQL                       //
////////////////////////////////////////////////////////

// this add a time and date to MySQL and looks like this in the MySQL table "2010-01-06 23:17:28"
// that is a TIMESTAMP field in MySQL, newer version of mysql store a timestamp like this "2010-01-06 23:17:28" instead of like this "1262819848"
$sql = "INSERT INTO users (`id`, `datetime`) VALUES ('', UTC_TIMESTAMP())";
executeQuery($sql);

// So if I query this record and try to show it like I do in the code above in the "Get mysql result section" It will not work correctly because  
// because it is like this "2010-01-06 23:17:28" instead of like this "1262819848"
// The ONLY solution I have found is to store the UTC timestamp into an INTEGER field instead of a DATETIME or TIMESTAMP field.
// Everyone on this site says this is wrong and I could use the default date and time stuff but I have yet to get it working.
// If I could store a timestamp in mysql like a timestamp with no "-" dashes in it, then it would work correctly when I show it on page.
// Basicly I have this half done, I can show the time and date in a users timezone as long as I pass the PHP a real TIMESTAMP like this "1262819848"

?>
根据过去对一个类似问题的回答,再加上阅读本网站上每一篇与php/mysql和时区相关的帖子,我可能无法解决这个问题,所以我会尽快开始悬赏。谢谢

如果我能通过2010-01-06 23:17:28 直到今天M:d:y h:i:s A、 $time_邮票

可能是:日期:m:d:y h:i:s A',STROTTIME'2010-01-06 23:17:28'

如果我能通过2010-01-06 23:17:28 直到今天M:d:y h:i:s A、 $time_邮票


可能使用:date'm:d:y h:i:sa',strotime'2010-01-06 23:17:28'

PHP中的strotime是将数据库datetime格式传递到PHP中的UNIX epoch时间戳的一种方法。它读取可理解的标准格式,并转换为UNIX epoch时间戳,以后php中的其他datetime函数(如date)可以使用该时间戳

备选方案可以是:

MySQL中的UNIX_TIMESTAMP函数可以帮助您将特定的列datetime或TIMESTAMP值转换为UNIX TIMESTAMP。正如上面提到的,它可以更快一些,因为这是在MySQL服务器端完成的

这似乎更像是一个时间戳而不是时区问题

请参阅更多:


PHP中的strotime是一种将数据库datetime格式传递到PHP中UNIX历元时间戳的方法。它读取可理解的标准格式,并转换为UNIX epoch时间戳,以后php中的其他datetime函数(如date)可以使用该时间戳

备选方案可以是:

MySQL中的UNIX_TIMESTAMP函数可以帮助您将特定的列datetime或TIMESTAMP值转换为UNIX TIMESTAMP。正如上面提到的,它可以更快一些,因为这是在MySQL服务器端完成的

这似乎更像是一个时间戳而不是时区问题

请参阅更多:


首先,如果你有一个编程问题的解决方案,我的建议是:使用它。不要担心让画廊相信你的解决方案的正确性

话虽如此,以下是我的两点:

向用户显示时区中的日期是一个显示/前端问题。代码和数据库中的内部日期应标准化为一个公共时区,然后转换为显示的用户时区


不要将日期\默认\时区\设置为用户提供的时区,而是将脚本的默认时区设置为UTC,使其与MySQL时间戳/日期时间值匹配,然后将用户的时区偏移量应用于标准化日期。如果用户提供日期时间,如$mydate=01/01/2010 5:30pm,则将用户的tz偏移量应用于该日期,并使用strtotime将其转换为标准tz,如strtotime$mydate$mytimezone,其中$mytimezone是用户提供的时区或偏移量

首先,如果你有一个编程问题的解决方案,我的建议是:使用它。不要担心让画廊相信你的解决方案的正确性

话虽如此,以下是我的两点:

向用户显示时区中的日期是一个显示/前端问题。代码和数据库中的内部日期应标准化为一个公共时区,然后转换为显示的用户时区

不要将日期\默认\时区\设置为用户提供的时区,而是将脚本的默认时区设置为UTC,使其与MySQL时间戳/日期时间值匹配,然后将用户的时区偏移量应用于标准化日期。如果用户提供类似$mydate=01的日期时间/
2010年1月5日下午5:30将用户的tz偏移量应用于该日期,并使用strtotime将其转换为标准tz,类似于strtotime$mydate$mytimezone,其中$mytimezone是用户提供的时区或偏移量

根据我两天的研究,最好的方法就是这样。但是有一点修改。如果您使用timestamp字段作为日期列,则根据用户的时区同步php应用程序时区和mysql服务器时区。然后,你不需要处理转换。根据我两天的研究,最好的方法是这样。但是有一点修改。如果您使用timestamp字段作为日期列,则根据用户的时区同步php应用程序时区和mysql服务器时区。然后,您不需要处理转换。