Dana
Dana

Reputation: 123

how to convert from MySQL timestamp to PHP Y-m-d?

I have next code:

//$item['Update']="2015-02-16 16:03:13"; value from debug,from mysql
date("Y-m-d", strtotime($item['Update']));

Why is it return 1970-01-01 ?

Problem is resolved,data from mysql was empty.

Upvotes: 1

Views: 339

Answers (3)

HashSu
HashSu

Reputation: 1517

echo date("Y-m-d", strtotime($item['Update']));

Also check this out (to do it in MySQL way.)

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

Updated:

$item['Update'] = '2015-02-16 16:03:13';
$date = new DateTime($item['Update']);
echo $date->format('Y-m-d'); // 2015-02-16

Output for Both statements:

Date is 2015-02-16

Upvotes: 1

cmorrissey
cmorrissey

Reputation: 8583

Since you are not really converting the format of the date and just stripping the time off it would be more efficient to use substr

<?php
    $item['Update']="2015-02-16 16:03:13";
    echo substr($item['Update'],0,10);
?>

Upvotes: 0

Santosh Jagtap
Santosh Jagtap

Reputation: 1075

Use this with single quotes rather than double quotes

$item['Update']='2015-02-16 16:03:13'; 
echo date("Y-m-d", strtotime($item['Update']));

Upvotes: 0

Related Questions