0

I've had a very good search on this topic but haven't managed to turn up a definitive answer that gives me what i need. What i'm trying to do is convert as follows (in PHP):

From: '2014-04-16 08:22:00.000' To: '16/04/2014 08:22'

And then back again, does anyone have an idea as to how this might be achieved? I not using seconds so i don't need that portion of the format only d/m/Y m:i. The original format comes from an MSSQL DB datetime field and the converted format will show in a input field.

Many thanks in advance.

Dan Hall
  • 1,474
  • 2
  • 18
  • 43

3 Answers3

1

DateTime::createFromFormat is your friend.

$date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2014-04-16 08:22:00.000');
echo $date->format('d/m/Y H:i');

OR

$date1 = new DateTime('2014-04-16 08:22:00.000');
echo $date1->format('d/m/Y H:i');
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
0

From DATETIME to PHP Date Formting:

date("WHATEVER", strtotime($mysql_result->date))

To DATETIME from date:

date('Y-m-d H:i:s', strtotime($time))

https://php.net/manual/en/function.date.php

https://php.net/manual/en/function.strtotime.php

Jono20201
  • 3,215
  • 3
  • 20
  • 33
  • Thanks, but found the following best: $date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2014-04-16 08:22:00.000'); echo $date->format('d/m/Y H:i'); – Dan Hall Apr 16 '14 at 14:55
0

Try to find your answer on google before: http://www.php.net/manual/en/datetime.formats.date.php

or

Convert one date format into another in PHP

Community
  • 1
  • 1
SupimpaAllTheWay
  • 1,308
  • 3
  • 16
  • 22
  • Agreed, i hate to mooch answers but really had a few hours of frustration with many different methods found on many different answers. Just really needed the most efficient method available. Abhiks answer is exactly that and will hopefully help other looking for a solution with my exact problem. – Dan Hall Apr 16 '14 at 14:54
  • Sure, glad you could found your answer – SupimpaAllTheWay Apr 16 '14 at 14:56