Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a mysql datetime in PHP to m/d/y format?

Tags:

php

datetime

I am trying to convert a mysql DATETIME into this format m/d/y but the code below is not working, returns 12/31/1969 instead can someone show me how to do this?

$fromMYSQL = '2007-10-17 21:46:59'; //this is the result from mysql DATETIME field
echo date("m/d/Y", $fromMYSQL);
like image 651
JasonDavis Avatar asked Aug 28 '09 02:08

JasonDavis


2 Answers

I think what you really want is this:

$fromMYSQL = '2007-10-17 21:46:59';
echo date("m/d/Y", strtotime($fromMYSQL));

The second argument of date is a timestamp and I think what is happening is PHP sees your string as a -1 timestamp... thus 12/31/1969.

So, to get a timestamp from the string version of the date, you use strtotime

like image 79
Doug Hays Avatar answered Sep 28 '22 17:09

Doug Hays


SQL:

SELECT whatever, UNIX_TIMESTAMP(date) date FROM table WHERE whatever

PHP:

date('m/d/Y', $result['date']);
like image 25
moo Avatar answered Sep 28 '22 16:09

moo