I feel stupid asking - but I've had a problem with my time format. Using php and pulling date/time from a MySQL db I am trying to format a time display so there are no leading zero characters in my 12 hour format. A space is fine.. Sounds easy right. Probably is...
Style I want is : 6:00 PM
NOT 06:00 PM
$result[0][0]
is the value of "2013-06-06 18:00:00"
PHP code is now:
echo strftime('%I:%M %p', strtotime($result[0][0]));
//upper case "i" WORKS but has leading zero
PHP code I think I need is :
echo strftime('%l:%M %p', strtotime($result[0][0]));
//Lower case "L" Provides no output
Latter is because according to php manual:
using %l
(lower-case 'L') yeilds Hour in 12-hour format, with a space preceding single digits 1 through 12
But when I use this format I get nothing!
What SIMPLE thing am I doing wrong?
(Hmmm , funny how the codes above look exactly alike in the StackOverflow screen but I am typing Upper case i and lower case L... in fact here is upper i --> "I", and here is lower L -->"l" ??) They look identical in this font??
$time = '23:45'; echo date('g:i a', strtotime($time));
The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).
Based on your code, I would also try this to remove the leading zero:
The "g" in PHP, will display the hour without a leading zero.
Time: <?= echo strftime("g:i:sa", strtotime($result['time'])); ?>
DateTime doing the trick:
<?php
header('Content-Type: text/plain');
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2013-06-06 18:00:00');
echo $date->format('g:i A');
?>
Also, date()
doing it:
<?php
header('Content-Type: text/plain');
echo date('g:i A', strtotime('2013-06-06 18:00:00'));
?>
Both show:
6:00 PM
strftime()
looks buggy though. %l
shows empty string. I tried to put setlocale(LC_TIME, "de_DE");
at the top, but it does not help.
Following might be a reason (from docs):
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 range may be limited to no earlier than the Unix epoch. This means that %e, %T, %R and, %D (and possibly others) - as well as dates prior to Jan 1, 1970 - will not work on Windows, some Linux distributions, and a few other operating systems. For Windows systems, a complete overview of supported conversion specifiers can be found at » MSDN.
On MSDN there is no %l
format code. It might be a reason, that killing return value. In this case strftime()
is platform-dependent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With