crnm
crnm

Reputation: 486

strftime gets wrong date

I'm using strftime to display future date.

But while using

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

I'm getting Mo., 01. April 2013 instead of Su., 31. March 2013 while using this today.

(Unix timestamp is 1364423120)

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

displays the correct Sa., 30. March 2013

What is wrong here with the last day of March?

Upvotes: 6

Views: 4386

Answers (5)

Timo Huovinen
Timo Huovinen

Reputation: 55613

Terje D. correctly mentions the confusion that you were getting with DST. But I also wanted to mention a way to do it 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

Upvotes: 0

Sammitch
Sammitch

Reputation: 32232

Check your timezones.

Use the below code to see the time, timestamp, and timezone of the date produced by your code.

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

edit:

If this is indeed a problem with DST in your area [North America's change was a couple weeks ago], and you're only interested in the 'day' part of the date then I would advise using date_add() instead of simple arithmetic since it will take into account DST changes, and other peculiarities of timekeeping.

Upvotes: 1

Terje D.
Terje D.

Reputation: 6315

The timestamp represents 23:25:20 local time. As daylight savings time comes into effect on March 31th, adding 96 h will give 00:25:20 as local time, thus a date 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)

Upvotes: 5

ducin
ducin

Reputation: 26437

When I ran

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

I got

Sun., 31. March 2013

So this day truly exists :) I think it's connected to the daylight saving time change that happens on that day. And when you're using whole day (24 hours multiplying), you're skipping the timezone change.

Upvotes: 1

Starx
Starx

Reputation: 78941

According to the manual

strftime — Format a local time/date according to locale settings

Its better if you specify a locale while you are using it, to avoid such problem.

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

Upvotes: 1

Related Questions