I have a timestamp that looks like this 2018-09-18T21:49:16Z from an API. I need to reformat the timestamp to appear in 09-18-2018 @ 21:49:16. How can I do this in php?
Asked
Active
Viewed 38 times
0
saladCracker
- 69
- 1
- 8
-
1Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Qirel Sep 19 '18 at 16:23
2 Answers
2
You can do this using DateTime::format():
$dateTime = "2018-09-18T21:49:16Z";
$DateTimeFormat = DateTime::createFromFormat("Y-m-d\TH:i:s\Z", $dateTime);
echo $DateTimeFormat->format("m-d-Y @ H:i:s");
// 09-18-2018 @ 21:49:16
You'll have to convert your date and time string (2018-09-18T21:49:16Z) to a DateTime object using DateTime::createFromFormate().
Tom Udding
- 2,234
- 3
- 22
- 28
0
The PHP datetime suite is your friend.
$mytime = new DateTime('2018-09-18T21:49:16Z');
echo $mytime->format('m-d-Y @ H:i:s');
Which results in:
09-18-2018 @ 21:49:16
Dave
- 4,794
- 16
- 30
- 38