Format Date field in MySQL using PHP to displayed in table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidhopkins
Forum Commoner
Posts: 41
Joined: Thu Jun 10, 2010 7:52 am

Format Date field in MySQL using PHP to displayed in table

Post by davidhopkins »

Hello all,

I have a MYSQL table that has a column called "created" which is a Date type. I then have a simple PHP script that is trying to access this date and convert it into a more readable format.

Currently it outputs at 2011-03-28 but i want it to look like 28 Aug 2011

The php code i have tried is as follows

Code: Select all

	$mysqldate = date( 'd M Y', $row['created'] );
  	echo $mysqldate;
Although this returns 01 Jan 1970 which is the right format but it hasnt used the value from my database

Any help would be great !

Thanks David
dgreenhouse
Forum Newbie
Posts: 20
Joined: Tue Mar 10, 2009 5:13 am

Re: Format Date field in MySQL using PHP to displayed in tab

Post by dgreenhouse »

Code: Select all

preg_match('/(\d{4})-(\d{2})-(\d{2})/','2011-03-28',$matches);

echo '<pre>';
print_r($matches);
echo '</pre>';

$timestamp = mktime(0,0,0,$matches[2],$matches[3],$matches[1]);

echo date( 'd M Y', $timestamp );

// Outputs:
Array
(
    [0] => 2011-03-28
    [1] => 2011
    [2] => 03
    [3] => 28
)
28 Mar 2011
Post Reply