I am pulling in a time from a database in the following format 10:00:00
I would like to be able to display this in the following format... 10am
Here's my twig code ...
{{ item.item_start_time }}
Any ideas how i can to this?
You can follow the twig documentation. It should be:
{{ item.item_start_time|date("ha") }}
For the format specifiers which I'm using, please refer to the documentation of date
date_default_timezone_set('Asia/Kolkata');
$date = '14:00:00';
echo date('hA', strtotime($date));
echo "</br>";
echo date('HA', strtotime($date));
Output
02PM
14PM
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
Reference date function
Reference Answer on StackOverflow Click Here
Related
I can't seem to get this date format correct.
I'm looking to display the current date like this:
Wednesday, May 16th
Any help would be appreciated.
Thanks :)
echo date('l,M jS');
php manual
http://php.net/manual/en/function.date.php
If you want leading zeroes (e.g. "Wednesday, May 02nd"):
date("l, F sS");
If you do not want leading zeroes (e.g. "Wednesday, May 2nd"):
date("l, F jS");
I am echoing out a date with the following code:
<?php echo date('h:i A', strtotime($catch[0]['catch_date'])); ?>
If I add it at 1:50PM it shows as: 1:50AM when echo'd out instead of showing PM.
I am stumped on this one. Any ideas?
Dates are being entered at the time of addition as:
'catch_date' => date('Y-m-d h:i:s')
You are using the wrong format.
From the manual:-
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
So you need to change your code to:-
'catch_date' => date('Y-m-d H:i:s')
You should set the default time zone before printing out the date. Example:
date_default_timezone_set('Pacific/Auckland');
And here is a full list of supported time zones:
https://www.php.net/manual/en/timezones.php
Trying to output the full month name using DateTime object but somehow I'm not interpet documentation show here .
I want the date out put a date like 18 November 2012 but Im not sucseeding
$date = new DateTime();
$datum = $date->format("d m Y");
outputs 18 11 2012 ??
reading on this format dd ([ \t.-])* m ([ \t.-])* y should output correct but this does not work at all ??
For the formatting characters, see the date() manual page. This is referred to several times on the DateTime::format() manual page.
For the full month name you want to use the formatting character F.
A full textual representation of a month, such as January or March
The "Date Formats" manual page that you linked to is for the input date string, not the output format.
I need to convert this date:
10.04.2011 19:00
To a date variable that I can use in PHP.
Can someone help me with that? I tried this way:
$dateConverted = date("d.m.Y H:i",strtotime ($date));
But it returns 01.01.1970 00:00
DateTime::createFromFormat() to the rescue!
It looks like your format is d.m.Y H:i.
So, this should work for you:
$dt = DateTime::createFromFormat('d.m.Y H:i', '10.04.2011 19:00');
echo $dt->format('Y-m-d H:i:s');
You should also take a look at the formats that strtotime and DateTime operate on. In particular, the reason that date didn't parse in strtotime is that it only expects dots as delimiters between Y, M and D if the year is only two digits. That's an odd one, don't look at me, it's not my fault.
I tried
$dtToday = DateTime::createFromFormat('Y-m-d', date('Y-m-d'));
but when I output it
die($dtToday->format('d M Y g:i:s a'));
I still get the time eg "22 Jan 2011 4:53:59 pm". Why is that?
UPDATE
Ah... many people misunderstood me, my bad, I forgot to point out the main point. I created the date with just the date portion, I don't want the time. So I'd expect something like
22 Jan 2011 12:00:00 am
You can call ->setTime(0, 0) to zero out the time portion:
$date = DateTime::createFromFormat('Y-m-d', '2011-01-22')->setTime(0, 0);
echo $date->format('d M Y g:i:s a');
// 22 Jan 2011 12:00:00 am
See the documentation for DateTime::createFromFormat:
If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.
If you do the following function call, you'll get the result you expect:
$dtToday = DateTime::createFromFormat('!Y-m-d', date('Y-m-d'));
Today's start timestamp
$todayStartTS = strtotime(date('Y-m-d', time()) . ' 00:00:00');
You can do this by passing the current unix timestamp as the second parameter to the date function
echo date("Y-m-d H:i:s",time());
Remove this part g:i:s a from your code.
Now, if you want a nice date formatted according to your local, i recommand you to use strftime() function.
You are getting "22 Jan 2011 4:53:59 pm" because those are the rules you format your date with :
d (day) : 22
M (Month) : Jan
Y (Year) : 2011
g (12-hour format) : 4
i (minutes): 53
s (seconds): 59
a (am/pm): pm
Be more speciffic about the format would you like your timestamp to have.
I suggest you take a peak at the php date documentation.
Is it using UTC, or something?
I have a PHP version that gives me an error whenever I do something date related without first using date_default_timezone_set. Maybe that'll help you.