How to convert the time from AM/PM to 24 hour format in PHP?
Try with this
echo date("G:i", strtotime($time));
or you can try like this also
echo date("H:i", strtotime("04:25 PM"));
Convert time from AM/PM to 24 hour
Assuming the time string format will be same -
$time = explode(' ', '5:4 pm');
$temp = date_parse($time[0]);
$temp['minute'] = str_pad($temp['minute'], 2, '0', STR_PAD_LEFT);
echo date('H:i a', strtotime($temp['hour'] . ':' . $temp['minute'] . ' ' . $time[1]));
Output
17:04 pm
How do you convert between 12 hour time and 24 hour time in PHP?
// 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"));
Convert date time with AM PM to 24 hour date format
I don't know what the origin of that date is (how it came to that), but you can use DateTime
classes for this:
$raw_date = '10/25/2014 14:00 PM';
// to disregard that PM escape it in the format
$new_date = DateTime::createFromFormat('m/d/Y H:i \P\M', $raw_date);
echo $new_date->format('Y-m-d H:i:s'); // 2014-10-25 14:00:00
How do I convert date/time from 24-hour format to 12-hour AM/PM?
I think you can use date()
function to achive this
$date = '19:24:15 06/13/2013';
echo date('h:i:s a m/d/Y', strtotime($date));
This will output
07:24:15 pm 06/13/2013
Live Sample
h is used for 12 digit time
i stands for minutes
s seconds
a will return am or pm (use in uppercase for AM PM)
m is used for months with digits
d is used for days in digit
Y uppercase is used for 4 digit year (use it lowercase for two digit)
Updated
This is with DateTime
$date = new DateTime('19:24:15 06/13/2013');
echo $date->format('h:i:s a m/d/Y') ;
Live Sample
Convert time from AM/PM to 24 hour
Assuming the time string format will be same -
$time = explode(' ', '5:4 pm');
$temp = date_parse($time[0]);
$temp['minute'] = str_pad($temp['minute'], 2, '0', STR_PAD_LEFT);
echo date('H:i a', strtotime($temp['hour'] . ':' . $temp['minute'] . ' ' . $time[1]));
Output
17:04 pm
Convert AM/PM to 24 hours clock in Laravel input field
You'll need to change your time field to the following
{{Form::time('time', \Carbon\Carbon::now()->timezone('Europe/Brussels')->format('H:i:s'),['class' => 'form-control'])}}
To explain the formatting;
H - 24 Hour with trailing zeros
i - Minutes with trailing zeros
s - Seconds with trailing zeros
You can see all the date/time variables at PHP Date
convert AMPM to datetime 24 hour
If you know the format of the string you better use the date_create_from_format
function:
$s = '01-31-2017 09:01 AM';
$date = date_create_from_format('m-d-Y h:i A', $s);
var_dump($date->format('Y-m-d H:i:s'));
When you use the strtotime
you let the php parse the string, and it might lead to a result you are not looking for (for example - 01-02-2017
- is it jan 2nd or feb 1st?).
Converting an AM/PM time to 24 hours format using PHP or MySQL?
STR_TO_DATE( `column_here`, '%l:%i %p' )
Read more here.
EDIT
Edited '%l:%M %p'
to '%l:%i %p'
Date and time in 24 hours format
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");
Related Topics
Using Comparison Operators in a PHP 'Switch' Statement
Minifying Final HTML Output Using Regular Expressions with Codeigniter
Get Code Line and File That's Executing the Current Function in PHP
Troubleshooting "No Such File or Directory" When Running 'PHP App/Console Doctrine:Schema:Create'
Youtube API V3: How to Find a List of Each 'Videocategoryid'
JSON_Encode Produce JSON_Error_Utf8 from Mssql-Select
Migrating Pure PHP Project to Yii Framework
How to Retrieve All Variables from a Twig Template
Codeigniter Back Button After Logout
Where Exactly Do I Put a Session_Start
What's the Correct Way to Set Env Variables in Laravel 5
Orm/Dao/Datamapper/Activerecord/Tablegateway Differences
Storage in Laravel Says Symlink - No Such File