0

I'm attempting to convert user inputted 12 hour time to 24 hour time in php so I can better sort and report on it. below code returns 16:00 no matter what is entered.

$starttime1 = date("H:i", strtotime("$starttime"));

in this instance what is being fed to $starttime is g:i:s:A or 4:00:00:AM

In the last hour of searching it appears to be an issue with the date function not getting the right information, so I tried DateTime::createFromFormat

$starttime = DateTime::createFromFormat('g:i:s:A', $starttime);
  $starttime1 = $starttime->format( 'H:i');

generates null

I feel like I'm doing something stupid.. need to take a break from coding and will keep googling, but hoping someone can point me in the right direction.

1 Answer 1

1

That's probably because you have a :AM in your time string. It should be a space instead of the :

$time = '4:00:00 AM';
$time = date('H:i', strtotime($time));

And the var_dump of $time would be:

string(5) "04:00"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.