Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strftime gets wrong date

Tags:

date

php

strftime

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?

like image 250
crnm Avatar asked Mar 27 '13 22:03

crnm


People also ask

What can I use instead of Strftime?

Replacements. For locale-aware date/time formatting, use IntlDateFormatter::format (requires Intl extension). In a real-life and ideal use case, the IntlDateFormatter object is instantiated once based on the user's locale information, and the object instance can is used whenever necessary.

What is Strftime short for?

strftime means string from time . we can format the time in different desirable ways.

What format is Strftime?

The strftime format is the standard date formatting for UNIX. It's used in C, Ruby, and more.


1 Answers

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)
like image 125
Terje D. Avatar answered Sep 19 '22 17:09

Terje D.