I have some dates in my DB and I want to show them in a 12 hour format with AM - PM at the end. I am using carbon, my code is this:
$hora = Carbon::createFromFormat('h:i A', $fecha_inicio, 'UTC')->setTimeZone($timeZone)->format('h:i A');
$fecha_inicio is something like 2018-11-02 13:47:03.
But this throws an error: ** Hour can not be higher than 12**
From the docs:
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, use H:i instead of h:i
$hora = Carbon::createFromFormat('Y-m-d H:i:s', $fecha_inicio, 'UTC')->setTimeZone($timeZone)->format('h:i A');
The first parameter from createFromFormat is the format you already have in $fecha_inicio
If you want the TIME in 12hr format
Carbon\Carbon::parse($fecha_inicio)->isoFormat('h:mm:i')
And if you want the TIME in 24hr format
Carbon\Carbon::parse($fecha_inicio)->isoFormat('H:MM:I')
Related
I want to create a DateTime object from format dd/mm/yyyy H:i:s, then I type:
DateTime::createFromFormat(
'dd/mm/yyyy H:i:s',
'01/02/2018 00:00:00'
);
And the result is false. Why and how to fix it?
Check the PHP: DateTime::createFromFormat for proper formatting.
d will try to match 7 or 07 day
m will match 1 or 01 month
Y will match 2017 (four-digit year)
The format string you should be using is d/m/Y H:i:s
Corrected:
DateTime::createFromFormat(
'd/m/Y H:i:s',
'01/02/2018 00:00:00'
);
Your Question on Why
The reason why it happened, is you are trying to say that your date actually has multiple months, multiple days, and multiple years:
DateTime::createFromFormat(
'dd/mm/yyyy H:i:s',
'01/02/2018 00:00:00'
);
So that action is expecting your input date to look like this:
'0101/0202/18181818 00:00:00'
But since your date did NOT fit that bill, it will return false as it fails to match the date to the format you provided.
Your Request for a Fix
The way to fix it, would be this instead (note a single d m and Y as per the documentation of PHP.net):
DateTime::createFromFormat(
'd/m/Y H:i:s',
'01/02/2018 00:00:00'
);
You used format 'dd/mm/yyyy H:i:s' which is not correct.
Use single letters such as:
d to represent two digit day 01 to 31 or 1 to 31,
m to get two digit month 01 through 12
Y uppercase as four digit year 1977 or 2017
H upper case as two digit hours 00 through 23
i to get two digit minutes 00 to 59
s to get two digit seconds 00 through 59
So correct format is 'd/m/Y H:i:s'
http://php.net/manual/en/datetime.createfromformat.php
I am taking input following Datetime : 12.01.2017 2:25 pm
But while I passed above datetime on php function strtotime then it's doesn't returning exact datetime as 24 hours format.
Here is my scripts:
$sdate="12.01.2017 2:25 pm";
date("d.m.Y H:m", strtotime($sdate))
It's returning always 12-jan-2017 14:01:00
But accurate output should be : 12-jan-2017 14:25:00
Please let me know how can get accurate datetime.
Thanks
hey there is problem on your dateformat string :
it should be H:i instead of H:m on your scripts.
Please correct date and format it accordingly your expectation:
date("d-M-Y H:i", strtotime($sdate));
<?php
$sdate="12.01.2017 2:25 pm";
echo date("d-M-Y H:i", strtotime($sdate));
?>
Output
You should use i instead of m in the date:
like this
echo $sdate="12.01.2017 2:25 pm";
echo date("d.m.Y H:i", strtotime($sdate));
There are already answers that use strtotime(). I would like to show you the DateTime class. I find it very usefull and better looking in general. Not that it matters to most people but still.
// Date string
$dateString = '12.01.2017 11:03 am';
// Create date object from string
$oDate = DateTime::createFromFormat('d.m.Y h:i a', $dateString);
// Output the date
echo $oDate->format('d.m.Y h:i a');
// Or some other format
echo $oDate->format('Y-m-d H:i:s');
Note that date() and DateTime accept the same date parameters. Your string suggests that you will need the following parameters.
d - Day of the month, 2 digits with leading zeros
m - Numeric representation of a month, with leading zeros
Y - A full numeric representation of a year, 4 digits
h - 12-hour format of an hour with leading zeros
i - Minutes with leading zeros
a - Lowercase Ante meridiem and Post meridiem
More information
For more information about date, DateTime, strtotime() and all parameters you can use:
date() - Manual
DateTime - Manual
strtotime() - Manual
date() and DateTime manuals list all possible parameters
I have a date in this format Fri, 15 Jan 2016 15:14:10 +0800, and I want to display time like this 2016-01-15 15:14:10.
What I tried is:
$test = 'Fri, 15 Jan 2016 15:14:10 +0800';
$t = date('Y-m-d G:i:s',strtotime($test));
echo $t;
But it is displaying date in this format: 2016-01-15 7:14:10, it should be 2016-01-15 15:14:10.
How can i do this?
Use H instead:
$test = 'Fri, 15 Jan 2016 15:14:10 +0800';
$t = date('Y-m-d H:i:s',strtotime($test));
echo $t;
H: 24-hour format of an hour with leading zeros 00 through 23
G should be the same, but without leading zeroes though. I suspect that your PHP is set to a different timezone than +0800. Can you confirm your timezone (date_default_timezone_get())?
EDIT
OP confirmed that his timezone was set to UTC, in which case it maskes perfect sense that it shows 7 in the morning, as date uses PHPs default timezone.
If you want to "inherit" the Timezone, while getting more flexibility, you should switch to DateTime:
echo (new DateTime($test))->format("Y-m-d H:i:s");
https://www.w3schools.com/php/php_date.asp
Get a Date
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
d - Represents the day of the month (01 to 31)
m - Represents a month (01 to 12)
Y - Represents a year (in four digits)
l (lowercase 'L') - Represents the day of the week
Get a Time
Here are some characters that are commonly used for times:
H - 24-hour format of an hour (00 to 23)
h - 12-hour format of an hour with leading zeros (01 to 12)
i - Minutes with leading zeros (00 to 59)
s - Seconds with leading zeros (00 to 59)
a - Lowercase Ante meridiem and Post meridiem (am or pm)
https://www.php.net/manual/en/datetime.format.php
public DateTime::format ( string $format ) : string
<?php
$datetime = new DateTime( "now", new DateTimeZone( "Europe/Bucharest" ) );
echo $datetime->format( 'Y-m-d H:i:s' );
I currently have stored DateTime in the database as follows 20130208110000 so I need to break it into 2 parts which is date in this format 08/02/2013 and time in this format 11:00 AM, which function does this effectively?
This is the function I used to join both date and time together
$startMonth = $this->_getParam('starts_month_day_year');
$startTime = $this->_getParam('starts_time');
date("YmdHis",strtotime($startMonth.$startTime));
<?php
$originalDate = "20130208110000";
$newDate = date("d-m-Y g:i A", strtotime($originalDate));
echo $newDate; //08-02-2013 11:00 AM
?>
where,
g :12-hour format of an hour without leading zeros [0 through 12]
i :Minutes with leading zeros [00 through 59]
A:Uppercase Ante meridiem and Post meridiem [AM or PM]
What would be wrong with just using what you're already doing?
$myDate = "20130208110000";
date("d/m/Y", strtotime($myDate)); // Date, e.g. 08/02/2013
date("h:i A", strtotime($myDate)); // Time, e.g. 11:00 AM
I am using following function and I want time in 24hr clock format but this gives me time in 12hrs:
<?php
date_default_timezone_set('Asia/Kolkata');
$timestamp = date("d/m/Y h:i:s", time());
print $timestamp ;
?>
What am I doing wrong?
From the docs for date(): The H format character gives the hour in 24h format. Also, you can use G if you do not want the leading 0 for hours before noon.
Examples (if current time was seven-something-AM)
date('H:i:s') -> "07:22:13"
date('G:i:s') -> "7:22:13"
For your specific case:
$timestamp = date("d/m/Y H:i:s", time());
According to the manual the difference is in the capitalization of hour portion:
"h" returns a 12-hour format of an hour with leading zeros, 01 through 12.
"H" returns a 24-hour format of an hour with leading zeros, 01 through 23.
According to the manual, G or H give you the time in 24-hour format. You should read the manual.
date('H', time());
The H format character gives the hour in 24h format. Also, you can use g if you do not want the leading 0 for hours before noon.
// 24-hour time to 12-hour time
$time_in_12_hour_format = date("g:i a", strtotime("13:30"));
// 12-hour time to 24-hour time
$time_in_24_hour_format = date("H:i", strtotime("1:30 PM"));