How to Convert Between 12 Hour Time and 24 Hour Time in PHP

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 12 hours to 24 hours in PHP

In your code there are few errors. The strpos syntax is wrong.

strpos("PM", $_a[2] !== FALSE) // this is incorrect

This you have to write

strpos($_a[2],"PM") //string first and search second.

This will return a integer, position of search string in the string, so don't use false instead use >-1

strpos($_a[2],"PM") > -1) //this is the correct method.

Also define $_time; in the starting and initialise it.

  <?php
$_a = ("07:29:23PM");
$_a = explode(':',$_a);
$_time = ""; //initialised the variable.
if($_a[0] == 12 && $_a[1] <= 59 && strpos($_a[2],"PM") > -1)
{
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos($_a[2],"PM")>-1)
{
$_a[0] += 12;
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] == 12 && $_a[1] <= 59 && strpos($_a[2],"AM" ) >-1)
{
$_a[0] = 00;
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos( $_a[2],"AM")>-1)
{
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
echo $_time;
?>

Actually, initialising variable was not causing the error. Error was in your strpos syntax, so none of the if condition was true, so no code executed, so while trying to echo $_time; it was undefined. But its good practice to initialise a variable in the starting itself.

How do I convert 12 hour clock time into 24 hour clock time in PHP?

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());

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 24 hour format hours into 12 hour format time in php

I think this will work for you.

date("h:i a",strtotime("1630"));

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"));

How to check date if its 12 hour format or 24 hour format in PHP?

You don't need regex to check if it is 24 hours or 12 hours. you can simply pass the date to strtotime function to get a timestamp of the date and then format the timestamp with date function.

$date="2021-02-5 11:45:00 AM";
$date=date('Y-m-j H:i:s',strtotime($date));
echo $date; // 2021-02-5 11:45:00

How do I convert a 12 hour format date/time string into 24 hours?

Try using the createFromFormat method on the DateTime object: php DateTime::createFromFormat

For example:

$newstartdate = DateTime::createFromFormat('d-m-Y H:i A' ,$start_date);

$start_date = $newstartdate->format('Y-m-d H:i:s') ;


Related Topics



Leave a reply



Submit