0

I've tried searching everywhere to find a solution for this but nothing I'm trying is working.

I have a date/time string in a 12 hour UK format:

20-07-2017 04:45 PM

I'm trying to convert that into the following 24 hour clock:

Y-m-d H:i:00

And here's the PHP I was trying to get to work...

$start_date = $_POST['startdate'];
$newstartdate = new DateTime($start_date);
$start_date = $newstartdate->format('Y-m-d H:i:00') ;

Am I misusing this? Should I be taking a completely diferent approach?

Metzed
  • 470
  • 1
  • 8
  • 27

1 Answers1

2

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') ;
NaN
  • 697
  • 5
  • 11
  • "d-m-Y H:i A" was the right format, don't know why you changed that. – JungleZombie Jul 15 '15 at 21:31
  • Yes @NaN that's it! That's the one I was missing. I've never heard of that, but I had a feeling I needed to tell it what the format the original date/time was in, otherwise how will it know which one is the month and which one is the day. Great, thanks! -- I was about to say you needed to add 'A' but you got there first :) – Metzed Jul 15 '15 at 21:31
  • @JungleZombie ah I couldnt test and wasn't overly sure the AM/PM would work, as you said it does I have now changed it back! – NaN Jul 15 '15 at 21:34
  • @Metzed Glad it is sorted – NaN Jul 15 '15 at 21:36