Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to format php time 12hr no leading zeros

Tags:

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

like image 811
eaton9999 Avatar asked May 28 '13 03:05

eaton9999


People also ask

How can change time in 24 hour format in PHP?

$time = '23:45'; echo date('g:i a', strtotime($time));

What is T and Z in time format?

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).


2 Answers

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'])); ?>

like image 24
jburnett Avatar answered Sep 19 '22 18:09

jburnett


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.

like image 106
BlitZ Avatar answered Sep 18 '22 18:09

BlitZ