Connecting Tech Pros Worldwide Help | Site Map

Converting MySQL dates

Tgone
Guest
 
Posts: n/a
#1: Sep 1 '06
Hello,

I need to convert a MySQL date "2007-03-08", into "March 8th, 2007".
Normally I would use MySQL to accomplish this task but I have to use
PHP.

Here's my code:

echo date("F jS, Y", $row['date']);

But this returns "December 31st, 1969". I'm not sure what's going on
here. Any ideas?

Chris Hope
Guest
 
Posts: n/a
#2: Sep 1 '06

re: Converting MySQL dates


Tgone wrote:
Quote:
I need to convert a MySQL date "2007-03-08", into "March 8th, 2007".
Normally I would use MySQL to accomplish this task but I have to use
PHP.
>
Here's my code:
>
echo date("F jS, Y", $row['date']);
>
But this returns "December 31st, 1969". I'm not sure what's going on
here. Any ideas?
date() expects a unix timestamp as the parameter and you're passing it a
string. Convert it to a unix timestamp either in the sql or using php's
strotime() function like so:

echo date("F jS, Y", strtotime($row['date']));

Note that when using unix timestamps for date and time functions you
have a limited date range from 1970 to 2038. Ideally it's best to do it
in the database query rather than in PHP.

In MySQL you would use the DATE_FORMAT() function which can be found on
the following page of the MySQL manual:
http://dev.mysql.com/doc/refman/5.0/...functions.html

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Tgone
Guest
 
Posts: n/a
#3: Sep 1 '06

re: Converting MySQL dates


Chris Hope wrote:
Quote:
Tgone wrote:
>
Quote:
I need to convert a MySQL date "2007-03-08", into "March 8th, 2007".
Normally I would use MySQL to accomplish this task but I have to use
PHP.

Here's my code:

echo date("F jS, Y", $row['date']);

But this returns "December 31st, 1969". I'm not sure what's going on
here. Any ideas?
>
date() expects a unix timestamp as the parameter and you're passing it a
string. Convert it to a unix timestamp either in the sql or using php's
strotime() function like so:
>
echo date("F jS, Y", strtotime($row['date']));
>
Note that when using unix timestamps for date and time functions you
have a limited date range from 1970 to 2038. Ideally it's best to do it
in the database query rather than in PHP.
>
In MySQL you would use the DATE_FORMAT() function which can be found on
the following page of the MySQL manual:
http://dev.mysql.com/doc/refman/5.0/...functions.html
Thanks! I tried the code and it works. I appreciate your advice.

Alvaro G. Vicario
Guest
 
Posts: n/a
#4: Sep 1 '06

re: Converting MySQL dates


*** Tgone escribió/wrote (31 Aug 2006 20:59:26 -0700):
Quote:
echo date("F jS, Y", $row['date']);
>
But this returns "December 31st, 1969". I'm not sure what's going on
here. Any ideas?
In order that this code works as expected, you need that your query looks
like this:

SELECT UNIX_TIMESTAMP(date) AS date FROM table


--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Closed Thread