13

Just a simple question. How do I convert a PHP ISO time (like 2010-06-23T20:47:48-04:00) to something more readable? Is there a function already built in PHP? I've looked around but I haven't seen anything to convert times. If there's not a function, is it possible?

Thank you

Dylan Taylor
  • 351
  • 2
  • 5
  • 12
  • you could trim the string from T or replace T with space and then trim from `-` (Z) if you need time. There is no other date more readable than ISO 8601 date format, specially if you are planning to share that date, worldwide. Everyone knows a date like `2020-10-09` means `9th Oct, 2020` but in some places of world `10/09/2020` is `10th Sep, 2020` and some other places `09th Oct, 2020` – AaA Mar 02 '20 at 03:30

7 Answers7

25
$format = "d M Y"; //or something else that date() accepts as a format
date_format(date_create($time), $format);
zebediah49
  • 7,467
  • 1
  • 33
  • 50
9

Try this:

echo date( "Y-m-d H:i:s", strtotime("2010-06-23T20:47:48-04:00") );

Format this part "Y-m-d H:i:s" using format from this documentation http://php.net/manual/en/function.date.php

Mladen Janjetovic
  • 13,844
  • 8
  • 72
  • 82
  • 1
    Worth mentioning that if you need to support dates prior to 1970 then strtotime will not be the way to go. date_create uses the DateTime classes of PHP and will support older dates as well. – strandmyr Nov 26 '18 at 09:15
  • @strandmyr It actually works fine with older dates. `strtotime()` will return negative number. Maybe on window 32-bit or something like that it can return false, but I am not sure – Mladen Janjetovic Nov 26 '18 at 14:22
1

Sounds like you're looking for the date function: http://php.net/function.date

Possibly paired with the strtotime function: http://php.net/function.strtotime

Jeffrey Blake
  • 9,659
  • 6
  • 43
  • 65
  • May work, but `date()` requires the timestamp to be an integer - therefore it won't work if the ISO time is already a string. – a_m0d Jun 24 '10 at 01:24
1

I think you should try strftime

http://php.net/function.strftime

Void
  • 1,502
  • 3
  • 16
  • 23
1

strptime() converts a string containing a time/date with the format passed as second argument to the function. The return value is an array containing values for day, month, year, hour, minutes, and seconds; you can use those values to obtain a string representing the date in the format you like.

strptime() is available since PHP 5.1.0, while the class DateTime is available since PHP 5.2.0.

apaderno
  • 28,547
  • 16
  • 75
  • 90
1
echo strftime("%b %e %Y at %l:%M %p", strtotime($ios));

will do

Raquibul Islam
  • 182
  • 1
  • 12
0

For a short date try this:

$a_date = '2010-06-23T20:47:48-04:00';
echo date('Y-m-d', strtotime($a_date));