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
Related
I have the following code:
$date = date_create('2014-11-06T23:00:00.000Z');
date_timezone_set($date, timezone_open('Australia/Victoria'));
echo $date ->format('Y-m-d H:i:s');
And it results in:
2014-11-07 10:00:00
However in the PHP doco here it says that capital H is 24hr time:
http://php.net/manual/en/function.date.php
H 24-hour format of an hour with leading zeros 00 through 23
If I comment out the middle line as follows:
$date = date_create('2014-11-06T23:00:00.000Z');
#date_timezone_set($date, timezone_open('Australia/Victoria'));
echo $date ->format('Y-m-d H:i:s');
It results in:
2014-11-06 23:00:00
Which does show 24hr time, but the date is a day out, which is why I need to set the timezone.
Why does setting the date_timezone_set stop me from being able to show the date in 24hr time format, and how to do fix this?
Unless you specify a timezone date_create assumes the current timezone. If you don't define one then it will use UTC.
Since the timezone is specified after the date_create the date is stored as UTC.
Since 2014-11-06 23:00 UTC is the same time as 2014-11-07 10:00 UTC+11 (Australia/Victoria) you will see the latter when you 'echo' the date.
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
I'm changing the date format and it shows me wrong format
echo $punch_in_time;
// Prints 2013-09-09 11:40:00
echo $new_date = gmdate('D, M-d-Y h:i a',strtotime($punch_in_time));
// Prints Mon, Sep-09-2013 09:40 am (Notice the wrong time)
I also tried to set the time zone before displaying the time, but no effect.
I don't know why this is happening, it must show my time as Mon, Sep-09-2013 11:40 am instead of Mon, Sep-09-2013 09:40 am.
besure to read the description/manual before you use a function.
it says "Format a GMT/UTC date/time" in the description of gmdate(), which means it is assuming the date you entered is in the local time zone (judging from the time difference GMT+2 ?) gmdate then convert it to a date format in GMT+0 time zone.
to make sure the timezone* is consistance between both input and output, use date() instead.
*this will convert the datetime to your local timezone, which might not be what you need.
echo $new_date = date('D, M-d-Y h:i a',strtotime($punch_in_time));
gmdate() identical to the date() function except that the time returned is Greenwich Mean Time
use this instead
echo $new_date = date('D, M-d-Y h:i a',strtotime($punch_in_time));
echo date('Y-m-d h:m:s',strtotime('2008-11-03 16:00:29'))
Returns 2008-11-03 04:11:29
I've tried changing my default time zone and adding GMT,UTC,PTC behind the string and nothing will change the output. How do I get strtotime to match the input?
Use 'Y-m-d H:i:s' if you want 24-hour time, or add the am/pm to the end with 'Y-m-d h:i:s a'
You need i for minutes and H for 24-hour.
See date documentation.
m is month, which is why your time has 11 as minutes, the same as the month.
I am writing a function to convert an internal date format into a timestamp value. However, when I print the date out in YYYY-MM-DD HH format the date is 12 hours off.
The code below gives me the wrong date and time. I am expecting 2011-03-25 13 but instead I am getting 2011-03-25 01.
date_default_timezone_set("Europe/London");
$epoch = mktime(13,0,0,3,25,2011);
echo date('Y-m-d h', $epoch);
When I use the following code I expect 2001-02-01 01 and get what I expected.
date_default_timezone_set("Europe/London");
$epoch = mktime(1,0,0,2,1,2011);
echo date('Y-m-d h', $epoch);
It seems that the 12 hour offset starts on March 25th in the 13th hour.
Any idea why this happens and how do I keep it from happening? Does this have to do with the different day light savings dates? The server timezone is set to "America/Los_Angeles".
It works, you're just using the wrong format code, take H (24 hour format) instead of h (12 hour format):
date_default_timezone_set("Europe/London");
$epoch = mktime(13,0,0,3,25,2011);
echo date('Y-m-d H', $epoch);
Read the PHP Manual, it explains each code in detail.