jonmrich
jonmrich

Reputation: 4323

Date formatting returning FALSE

I'm getting dates back like this from a mySQL query: 2017-06-23 20:08:58

I'm trying to do this to get the dates back in a simpler format:

$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
$items = array();
foreach ($response as &$value) {
    $items[] = date_format($value['date_added'], 'Y-m-d');
}

However, this returns only false for every date.

If I try

$response = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $items = array();
    foreach ($response as &$value) {
        $items[] = $value['date_added'];
    }

I get back the dates in this format (2017-06-23 20:08:58), so I know the query works, but how do I format these dates "on the fly?"

Upvotes: 1

Views: 382

Answers (1)

aynber
aynber

Reputation: 23001

date_format wants a DateTimeInterface object instead of a string. What you can do is use mysql's date_format in your query so you don't need to do it with PHP.

SELECT DATE_FORMAT(date_added, '%Y-%m-%d') AS date_added

Upvotes: 3

Related Questions