Different results for date () and gmdate ()

I found something that I canโ€™t explain, maybe someone here can give me a hint.

I have the following test code that prints 2 formatted timestamps, one for 03/31/2013 and one for 03/31/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";

The result surprises me:

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

Where does the difference in 2014 come from? My first thought is summer time, but why doesnโ€™t it affect 2013? Why is there a difference of 2 hours in 2014, but a difference of 1 hour in 2013?

+4
source share
4 answers

Daylight for Berlin begins with

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

- 00:00 , 2013 , 31 , 2 , ; 2014 - 2 30 .

+7

, . gmdate date

print "date:   " . date('d.m.Y', $timestamp) . "\n";
print "gmdate: " . gmdate('d.m.Y', $timestamp) . "\n";

print "date:   " . date('d.m.Y H:i:s', $timestamp) . "\n";
print "gmdate: " . gmdate('d.m.Y H:i:s', $timestamp) . "\n";

.

+3

DayLight?

this, , 2013-03-31 02:00:00 03:00:00

+2

gmdate() GMT/UTC date()

 <?php
   date_default_timezone_set("Asia/Bangkok");
   echo gmdate('Y-m-d H:i:s');
   echo date('Y-m-d H:i:s');
  ?>
0

Source: https://habr.com/ru/post/1530385/


All Articles