服务器strtotime不正确 [英] Server strtotime incorrect

查看:73
本文介绍了服务器strtotime不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

strtotime和mktime都输出了错误的时间戳,这让我发疯.

Both strtotime and mktime are outputting an incorrect timestamp and it's driving me mad.

如果我添加以下strtotime('2012-10-09');

If I add the following strtotime('2012-10-09');

我得到1349701200

I get 1349701200

实际上是哪个 2012年10月8日,星期一13:00:00 GMT

Which is actually Mon, 08 Oct 2012 13:00:00 GMT

我正在使用本地主机,正在运行MAMP.我以为这是服务器时区问题或其他问题,但我不知道为什么,或如何解决.

I'm using my localhost, running MAMP. I'm assuming it's a server timezone issue, or something, but I don't understand why, or how to fix it.

有帮助吗?

推荐答案

strtotime使用默认时区来解释字符串.如果您想要不同的时区,则可以明确指定它或为所有呼叫更改它:

strtotime uses default timezone to interpret the string. If you want different timezone you could specify it explicitly or change it for all calls:

<?php

if (date_default_timezone_get()) {
    echo  'date_default_timezone:  ' . date_default_timezone_get()."\n";
}

echo strtotime('2012-10-09')."\n"; # default timezone
echo strtotime('2012-10-09 UTC')."\n";
date_default_timezone_set('UTC');
echo strtotime('2012-10-09')."\n";
?>

输出

date_default_timezone:  Europe/London
1349737200
1349740800
1349740800

POSIX时间戳记从1970-01-01 00:00:00 UTC 开始的秒数.例如,在每年的这个时间,纽约的午夜(00:00)可能是UTC的20:00(相同的POSIX时间戳).但是UTC的00:00和纽约的00:00对应于不同的时刻(不同的POSIX时间戳).本地时钟通常(大致来说)跟随太阳,即使您所在的夜晚也是如此.太阳照耀着地球上的某个地方.

POSIX timestamp counts number of seconds since 1970-01-01 00:00:00 UTC. For example, midnight (00:00) in New York may be 20:00 in UTC at this time of year (the same POSIX timestamp). But 00:00 in UTC and 00:00 in New York correspond to different moments in time (different POSIX timestamps). Local clocks follow the Sun usually (roughly speaking) and even if it is night where you are; the Sun shines somewhere on Earth.

这篇关于服务器strtotime不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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