DateTime::format and strftime
11,454
setlocale(LC_TIME, 'it_IT.UTF-8');
$date = new DateTime($run['at']);
strftime("%d %B", $date->getTimestamp())
... worked. :)
Author by
MultiformeIngegno
Updated on July 08, 2022Comments
-
MultiformeIngegno almost 2 years
I have
$date = $run['at'];
which gives me 2013-06-03T16:52:24Z (from a JSON input). To transform it to get for example "d M Y, H:i" I use$date = new DateTime($run['at']); echo $date->format('d M Y, H:i');
Problem is I need the date in italian. And the only function that supports
set_locale
isstrftime
. How can I "wrap"DateTime::format
withstrftime
(or replace, dunno)? -
Robert almost 11 yearsIt was easy to find next time before posting a question do some research because this question was easy to be googled.
-
j4k3 over 4 years@Robert Easy to be googled, yet wrong. getTimstamp() makes the DateTime object lose its timezone. The correct way would be to output the DateTime in any format that strtotime can recognize and then pass that to strftime.
-
j4k3 over 4 yearsThis answer may seem right at first. But getTimstamp() makes the DateTime object lose its timezone. The output will be wrong whenever GMT is in another month than your local timezone. The correct way would be to strip the timezone by outputting the DateTime in any format that strtotime can recognize and then pass that to strftime.