1

I'm decoding a JSON response and outputting it in a simple table. Here is a sample of the code:

<?php

foreach($results['Events'] as $values)

{
        echo '<tr><td>' . $values['Title'] . '</td>';
        echo '<td>' . $values['Details']['Venue'] . '</td>';
        echo '<td>Event date:' . $values['Details']['Date'] . '</td></tr>';
}

?>

Since Date has a timezone added to it, this is what I get:

Event title | Event venue | Event date
Event one | Venue one | 2016-01-01T00:00:00
Event two | Venue two | 2016-01-02T00:00:00
Event three | Venue three | 2016-01-03T00:00:00

Is there a way to remove "T00:00:00" when echoing the results? This is a desired outcome:

Event title | Event venue | Event date
Event one | Venue one | 2016-01-01
Event two | Venue two | 2016-01-02
Event three | Venue three | 2016-01-03
1
  • 1
    For each date, create a new DateTime object, then format is as you want. Commented Sep 4, 2015 at 14:08

2 Answers 2

5

Use DateTime:

$date = new \DateTime($values['Details']['Date']);
echo '<td>Event date:' . $date->format('Y-m-d') . '</td></tr>';
Sign up to request clarification or add additional context in comments.

Comments

3

you can also use strtotime()

echo '<td>Event date:' . date("Y-m-d",strtotime($values['Details']['Date'])) . '</td></tr>';

2 Comments

Very nice and elegant solution, thank you. I have accepted bspellmeyer's answer because he was the first one to offer the solution.
nice to have a single line solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.