Convert date format.

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
gth759k
Forum Commoner
Posts: 76
Joined: Mon Jun 15, 2009 3:04 am

Convert date format.

Post by gth759k »

I have dates in a database table in the form of yxxx-mx-dx, which I want to display as Mxxxx dx, yxxx.
I'd like to have something like:

date('F, j, Y', mktime(0, 0, 0, Mx, Dx, Yxxx));

do any of you have any idea how to convert yxxx-mx-dx into array(Mx, Dx, Yxxx)?
Please help!
Thanks.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: Convert date format.

Post by spider.nick »

gth759k wrote:I have dates in a database table in the form of yxxx-mx-dx, which I want to display as Mxxxx dx, yxxx.
I'd like to have something like:

date('F, j, Y', mktime(0, 0, 0, Mx, Dx, Yxxx));

do any of you have any idea how to convert yxxx-mx-dx into array(Mx, Dx, Yxxx)?
Please help!
Thanks.
http://www.php.net/list
http://www.php.net/explode

Code: Select all

list( $year, $month, $day ) = explode( '-', $date );
Hope that helps!

Nick
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Convert date format.

Post by pickle »

Is the column of type 'date' in the database? If so, you can do this all in MySQL:

[sql]SELECT  CONCAT(MONTHNAME(`yourDateField`),' ',DAYOFMONTH(`yourDateField`),', ',YEAR(`yourDateField`)) AS 'formattedDate'FROM  `yourTable`[/sql]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Convert date format.

Post by requinix »

pickle wrote:[sql]SELECT  CONCAT(MONTHNAME(`yourDateField`),' ',DAYOFMONTH(`yourDateField`),', ',YEAR(`yourDateField`)) AS 'formattedDate'FROM  `yourTable`[/sql]
For that matter,

Code: Select all

SELECT DATE_FORMAT(`yourDateField`, "%M %e, %Y") AS `formattedDate`...
Post Reply