phyo
phyo

Reputation: 37

retrieve year and month from database

I want to retrieve year and month from my timestamp(eg.'2015-8-02 56:30:00) column like that '2015/5,2015/6,...' to show in row of table.But I cann't it .

Here is my query

$dateFormat = 'Y-m-d H:i:s';
$startDate = '2014-01-23 00:00:00';
$endDate=date('Y-m-d H:i:s');
$date = DateTime::createFromFormat($dateFormat, $startDate);
$year = $date->format('Y/m');

Upvotes: 0

Views: 51

Answers (1)

Alex Babak
Alex Babak

Reputation: 489

You can do it in MySQL query, like:

SELECT *, DATE_FORMAT( `timestampColumn`,  '%Y/%m' ) as `formattedDate` FROM `yourTable`;

Upvotes: 3

Related Questions