Jump to content

Convert a string from 12hour time format to 24hour format


Recommended Posts

Hello All, 
  Need an advise in finishing this task please. 
 

function convertTimeFormat($time12Hour) {
// Initialized required variable to an empty string.
		$time24Hour = ""; 

// Used explode() function to break the string into an array and stored its value in $Split variable. 
		$Split = explode(":",$time12Hour); // print_r(explode (":", $time12Hour)); => Array ( [0] => 09 [1] => 50 [2] => 08AM )

// Retrieved only "hour" from the array and stored in $Hour variable. 
		$Hour = $Split[0];   

    $Split[2] = substr($Split[2],0,2);

// Used stripos() function to find the position of the first occurrence of a string inside another string.	
		if($Hour == '12' && strpos($time12Hour,"AM")!== FALSE) {
			// Code here 
		} elseif(strpos($time12Hour,"PM")!== FALSE && $Hour != "12") {
			// code here
		}
		return  $time24Hour;
	}
	$time12Hour = "09:50:08AM";
	$result = convertTimeFormat($time12Hour);
	print_r($result); 

/*
Input : "09:50:08AM";
Output : "21:50:08PM";
*/

 

6 hours ago, ginerjm said:

If your input is a valid time string (or if not) there are plenty of date-related functions and formatting types that make your 'problem' a no-brainer.  RTM!

Though that's plausible but I try not to use date-related functions and formatting types. 

20 hours ago, Saggi said:

Hello All, 
  Need an advise in finishing this task please. 
 


function convertTimeFormat($time12Hour) {
// Initialized required variable to an empty string.
		$time24Hour = ""; 

// Used explode() function to break the string into an array and stored its value in $Split variable. 
		$Split = explode(":",$time12Hour); // print_r(explode (":", $time12Hour)); => Array ( [0] => 09 [1] => 50 [2] => 08AM )

// Retrieved only "hour" from the array and stored in $Hour variable. 
		$Hour = $Split[0];   

    $Split[2] = substr($Split[2],0,2);

// Used stripos() function to find the position of the first occurrence of a string inside another string.	
		if($Hour == '12' && strpos($time12Hour,"AM")!== FALSE) {
			// Code here 
		} elseif(strpos($time12Hour,"PM")!== FALSE && $Hour != "12") {
			// code here
		}
		return  $time24Hour;
	}
	$time12Hour = "09:50:08AM";
	$result = convertTimeFormat($time12Hour);
	print_r($result); 

/*
Input : "09:50:08AM";
Output : "21:50:08PM";
*/

 

function convertTimeFormat($time12Hour) {
		$time24Hour = ""; 
		$Split = explode(":",$time12Hour); 
		$Hour = $Split[0]; 
       // $Minutes = $Split[1];
       // $Seconds = $Split[2];
		$Split[2] = substr($Split[2],0,2);
		
		if($Hour == '12' && strpos($time12Hour,"AM")!== FALSE) {
		} elseif(strpos($time12Hour,"PM")!== FALSE && $Hour != "12") {
		} elseif($Hour != "12" && strpos($time12Hour,"AM")!== FALSE) {
            echo $convertHour = 12+$Hour.":".$Split[1].":".$Split[2]."PM";
		}
		return  $time24Hour;
	}
	$time12Hour = "06:40:03AM";
	$result = convertTimeFormat($time12Hour);
    

 

Why **ss about when there are inbuilt functions that can accomplish the task far more efficiently?

function reformatTime($tstr)
{
    $dt = DateTime::createFromFormat('h:i:sa', $tstr);
    return $dt->format('H:i:s');
}

echo reformatTime('08:30:00PM');     //--> 20:30:00

 

1 hour ago, Saggi said:

Though that's plausible but I try not to use date-related functions and formatting types. 

They are part of the language.  Whenever possible you should always use the built in types and functions unless you have a very good reason not to.  

  • Like 1
9 hours ago, Barand said:

Why **ss about when there are inbuilt functions that can accomplish the task far more efficiently?


function reformatTime($tstr)
{
    $dt = DateTime::createFromFormat('h:i:sa', $tstr);
    return $dt->format('H:i:s');
}

echo reformatTime('08:30:00PM');     //--> 20:30:00

 

function convertTimeFormat($time12Hour){
 echo date("H:i:s:A", strtotime("06:40:03 PM"));
}
$time12Hour="06:40:03PM";
echo $result=convertTimeFormat($time12Hour);

 

8 hours ago, gizmola said:

They are part of the language.  Whenever possible you should always use the built in types and functions unless you have a very good reason not to.  

I want to learn PHP the hard way. lol.  Anyway,  I finished the task by both using in built and traditional way. Thanks for the input. Appreciate it. 

A couple of comments on your code:

function convertTimeFormat($time12Hour){
 echo date("H:i:s:A", strtotime("06:40:03 PM"));
}

First off you are not using the parameter $time12hour.  Some smart editors will introspect and alert you to this.  You can also typehint your parameter and the return value.  Since you require $time12Hour to be a string,  it is helpful to add the typehint to the parameter. 

Also, with functions it is best practice to return a result.  You shouldn't echo the result, but instead return it. You can also typehint the return value, which should also be a string.

function convertTimeFormat(string $time12Hour): string
{
  return date("H:i:s:A", strtotime($time12Hour));
}

Now your example code becomes this:

$time12Hour="06:40:03PM";
echo convertTimeFormat($time12Hour);

You could also save the function result to a variable,.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.