PDA

View Full Version : PHP date()



NeoDreamer
May 28th, 2008, 02:28 PM
In my MySQL table, I have a timestamp column with values like "2008-05-28 00:39:40". First, I must remove all the formatting:


$delimeters = array('-', ' ', ':');
$time = str_replace($delimeters, '', $new['time']);
echo $time;
// 20080528003940 is displayed

Then I attempt to use the date function on this, but it gives me a weird date in the future:


echo date('M, j, Y', $time);
// Jan, 19, 2038 is displayed

Krilnon
May 28th, 2008, 03:06 PM
The second parameter is interpreted as the number of seconds since the Unix epoch, I think.

You could use strtotime:
$str = "2008-05-28 00:39:40";
echo date('M, j, Y', strtotime($str));

djheru
May 28th, 2008, 03:14 PM
You need to use php's strtotime to convert the mysql time to unix time.



$time = strtotime($new['time'];
echo date('M, j, Y', $time);

NeoDreamer
May 28th, 2008, 03:26 PM
Yup, that does the trick. Thanks.