0

How do I to convert a w3c date to a custom format inside PHP?

My w3c date: "2015-02-26T03:11:41.000-03:00" My custom format: "dd/mm/YYYY hh:mm:ss"

Has someone Any idea?

vinoli
  • 457
  • 1
  • 6
  • 20

4 Answers4

2

How about this?

$dateTime = new DateTime('2015-02-26T03:11:41.000-03:00');
echo $dateTime->format('d/m/Y h:i:s');

By using the codes above, it will output:

26/02/2015 03:11:41

It's always suggested to use DateTime class whenever available, which will have Exception to handle unable parsing cases.

Raptor
  • 53,206
  • 45
  • 230
  • 366
0

Check out the Docs for date()

// prints something like: Wednesday the 15th
echo date('l \t\h\e jS');

Check out the section about the parameters on the Docs link.

André Ferraz
  • 1,511
  • 11
  • 29
  • 1
    It is safer to use an instance DateTime() as it will allow you to using typehinting for all methods that manipulate dates, it makes modifying dates easier and more accurate and finally it allows you to change the timezone. – swifty Feb 26 '15 at 09:08
  • I wouldn't say safer, I would say more useful, just tried to made it simple as possible, will update my answer with that option. – André Ferraz Feb 26 '15 at 09:30
  • You are right, Safer might have been the wrong word. I think less margin for human error might have been a better way to have got my point across :-) – swifty Feb 26 '15 at 11:42
0

You can try:

$dateTime = new DateTime($dateVar);
$formatted = $dateTime->format("dd/mm/YYYY hh:mm:ss");
Raffy Cortez
  • 458
  • 3
  • 8
0

You can do it like this:

DateTime::createFromFormat(DateTime::W3C,  $your_w3c_date_here)->format('d/m/Y H:i:s');
Teneff
  • 30,564
  • 13
  • 72
  • 103