Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strftime doesn't show PM or AM

Tags:

php

strftime

This is the code I use to get the date (in spanish):

$fecha = strftime("%d de %B, %Y, a las %l:%M %p");

and I get:

27 de julio, 2011, a las 7:10

So the question is, where is my PM (or AM)?

My application is running on Linux.

like image 449
Danny Avatar asked Jul 28 '11 00:07

Danny


People also ask

What can I use instead of Strftime in PHP?

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 PHP?

strftime() function in PHP The strftime() function formats a local time/date according to locale settings. It returns a string formatted according to the given format string using the given timestamp or the current local time if no timestamp is given.


1 Answers

Well,, the php manual says

Not all conversion specifiers may be supported by your C library, in which case they will not be supported by PHP's strftime(). Additionally, not all platforms support negative timestamps, so your date

This could be your problem. In that case you could do something like this to get the AM/PM:

$time = time();
$fecha = strftime("%d de %B, %Y, a las %l:%M ", $time).(($time%86400) < 43200 ? 'AM' : 'PM');

EDIT:

This answer is assuming you want a UTC time string (if you want another timezone, you should also include the timezone in the string if you are storing it or sending it anywhere), and that your php.ini has date.timezone set to UTC . If you are not using UTC, you should compute an offset. See @Soylent17's answer.

like image 121
Paul Avatar answered Oct 24 '22 21:10

Paul