Strftime gets the wrong date

I use strftime to display the future date.

But when using

strftime('%a., %d. %B %Y',time()+60*60*24*4)

I get Mo., 01. April 2013 instead of Su., 31. March 2013, using it today.

(Unix timestamp - 1364423120)

strftime('%a., %d. %B %Y',time()+60*60*24*3)

Displays the correct Sa., 30. March 2013

What is wrong with the last day of March?

+5
source share
5 answers

The time stamp is 23:25:20 local time. Since daylight saving time will take effect on March 31, adding 96 hours will give 00:25:20 as local time, so the date is one day later than expected. Using gmstrftime instead of strftime avoids this problem.

<?php

$timestamp = 1364423120;

echo strftime('%a., %d. %B %Y (%c %Z)', $timestamp)."\n";
echo strftime('%a., %d. %B %Y (%c %Z)', $timestamp +60*60*24*4)."\n";
echo gmstrftime('%a., %d. %B %Y (%c %Z)', $timestamp)."\n";
echo gmstrftime('%a., %d. %B %Y (%c %Z)', $timestamp +60*60*24*4)."\n";

gives

Wed., 27. March 2013 (Wed Mar 27 23:25:20 2013 CET)
Mon., 01. April 2013 (Mon Apr  1 00:25:20 2013 CEST)
Wed., 27. March 2013 (Wed Mar 27 22:25:20 2013 GMT)
Sun., 31. March 2013 (Sun Mar 31 22:25:20 2013 GMT)
+5
source

Check your time zones.

, , , .

echo strftime('%s %H:%M:%S %z %Z %a., %d. %B %Y',time()+60*60*24*4);
//Output: 1364769859 15:44:19 -0700 PDT Sun., 31. March 2013

DST [ ], "" , date_add() , DST .

+1

strftime - /

, , .

setlocale(LC_TIME, "de_DE");
strftime('%a., %d. %B %Y',time()+60*60*24*4)
+1

echo strftime('%a., %d. %B %Y',time()+60*60*12*7)

Sun., 31. March 2013

, :) , , . ( 24 ), .

+1

Terje D. correctly mentions the confusion you are facing with DST. But I also wanted to mention a way to do this with DateTime, which in my personal opinion is easier to understand and use.

$dt = new DateTime('now', new DateTimeZone('UTC')); // creates a datetime object representing the current time in UTC
$dt->modify('+3 days'); // add 3 days to the datetime object
echo $dt->format('Y-m-d H:i:s')."\n"; // This will give you the time in english independent of the locale settings on your server
echo strftime('%a., %d. %B %Y', $dt->getTimeStamp()); // this will give you the time in your locale, which as mentioned before, will include DST
0
source

All Articles