date()和gmdate()的不同结果 [英] Different results for date() and gmdate()

查看:85
本文介绍了date()和gmdate()的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一些我不能真正解释的东西,也许有人可以给我一个提示。

I found something I can't really explain, maybe someone here can give me a hint.

我有以下测试代码,打印2个格式化的时间戳,一个为31.03.2013,一个为31.03.2014,使用 date() gmdate()

I have the following test code, that prints 2 formatted timestamps, one for the 31.03.2013 and one for 31.03.2014, using date()and gmdate():

<?php

function print_date($timestamp, $year) {
  // Add timezone offset for germany
  $timestamp += 3600;
  print "in $year\n";
  print "date:   " . date('d.m.Y H:i:s', $timestamp) . "\n";
  print "gmdate: " . gmdate('d.m.Y H:i:s', $timestamp) . "\n";
  print "\n";
}

$end_2013 = 1364684400; // 31.03.2013
$end_2014 = 1396216800; // 31.03.2014
print_date($end_2013, '2013');
print_date($end_2014, '2014');

print "Default timezone: " . date_default_timezone_get() . "\n";

结果令我惊讶:

in 2013
date:   31.03.2013 01:00:00
gmdate: 31.03.2013 00:00:00

in 2014
date:   31.03.2014 01:00:00
gmdate: 30.03.2014 23:00:00

Default timezone: Europe/Berlin

2014年的差异来自哪里?我的第一个想法是夏令时,但为什么不在2013年有效?
为什么2014年有两个小时的差异,2013年只有1小时的差距?

Where does the difference in 2014 come from? My first thought is daylight savings time, but why doesn't that have an effect in 2013? Why are there 2 hours difference in 2014 but only 1 hour difference in 2013?

推荐答案

2013 Sunday, 31 March, 02:00 
2014 Sunday, 30 March, 02:00 

每个日期的指定时间值为00:00,因此2013年3月31日星期日在上午2点之前是,所以没有夏令时在2014年3月30日凌晨2点之后

Your specified time value for each date is 00:00 on that date, so for 2013 Sunday, 31 March it is before 2am, so no daylight savings; for 2014 it is after 2am on 30th March

这篇关于date()和gmdate()的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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