Changing 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
marcdd2
Forum Newbie
Posts: 4
Joined: Tue Apr 06, 2010 7:58 pm

Changing date format

Post by marcdd2 »

I have a variable that is set as a date in mySQL DB that is pulled from the DB, posted to the next page, and then displayed. if i just display the variable it comes out as 2010-06-20. If I try to change the format with date("m/d/y", $eventdate) it is displayed as 12/31/69. I assume the php engine doesn't think that 2010-06-20 is a valid date. Can someone help?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Changing date format

Post by AbraCadaver »

Well, it is not a valid unix timestamp which is what date() expects. Try: http://dev.mysql.com/doc/refman/5.1/en/ ... ate-format
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Changing date format

Post by John Cartwright »

AbraCadaver wrote:Well, it is not a valid unix timestamp which is what date() expects. Try: http://dev.mysql.com/doc/refman/5.1/en/ ... ate-format
Although I recommend what AbraCadaver has suggested, I just want to add why your solution probably isn't working. Basically, mysql timestamps are not the same as a unix timestamp (YYYYMMDD DDMMSS vs seconds since epoch Dec 31 1969). Therefore, you need to convert your mysql timestamp to a unix timestamp if you want to format the string using php's date function, which simply involves passing mysql timestamp through strtotime() and passing that to the date() function instead.
Post Reply