0

I have a string that contains a "json timestamp" (I guess its called so, because of this question: The "right" JSON date format)

2015-08-11T09:19:25Z

Now I want to convert this string with php into something like this:

09:19 11.08.2015

I tried the date function, but it did not work.

Community
  • 1
  • 1
Zoker
  • 2,020
  • 5
  • 32
  • 53
  • 1
    have you seen in the other question ? > "There is no date format in JSON" – mmm Aug 11 '15 at 18:48
  • Yea but also "You should use the format emitted by Date's toJSON method" and because of the "toJSON" I thought its a json timestamp – Zoker Aug 11 '15 at 18:49
  • json has no "date" type, no time type. it has arrays, objects, integers, and strings, basically. your date is just a string. RTFM: http://php.net/date `date()` is for taking a unix timestamp and converting it to a human-readable date/time STRING. you want [date_create_from_format()](http://php.net/manual/en/datetime.createfromformat.php) which takes in a time STRING, and gives you back a date/time object. – Marc B Aug 11 '15 at 18:50

2 Answers2

5

You can use DateTime to convert an ISO date to a format you want:

$a = new \DateTime("2015-08-11T09:19:25Z");
$b = $a->format('H:i d.m.Y');
var_dump($b);
baao
  • 71,625
  • 17
  • 143
  • 203
0

if you want use date function from PHP, you must set correct timezone:

date_default_timezone_set('America/New_York');
var_dump(date("H:i d.m.Y", strtotime('2015-08-11T09:19:25Z'))); 
Jiri Zachar
  • 487
  • 3
  • 8