473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Converting MySQL dates

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?

Sep 1 '06 #1
3 1891
Tgone wrote:
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
Sep 1 '06 #2
Chris Hope wrote:
Tgone wrote:
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.

Sep 1 '06 #3
*** Tgone escribió/wrote (31 Aug 2006 20:59:26 -0700):
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
--
Sep 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kit | last post by:
Howdy! I have some dates in a MySQL database that are in a 'Y-m-d' format. Is there a way to have PHP read these dates and convert them to a 'd-M-Y' format?
3
by: Mark Stevens | last post by:
I am trying to do select all of the records in a table where they data has not expired. The table contains two fields Subject (varchar(100)) and ExpiryDate (Date). I am getting all of the...
5
by: duikboot | last post by:
Hi all, I'm trying to export a view tables from a Oracle database to a Mysql database. I create insert statements (they look alright), but it all goes wrong when I try to execute them in Mysql,...
0
by: Massimo Fiorentino | last post by:
Hello there! I am a bit of a newbee into the mySQL world and I have a question regarding switching from one DB to another. I have for a couple of years used a very simple CMS-system created by...
3
by: usenet | last post by:
I have inherited a table where date information was saved from PHP as a VARCHAR. (Sigh.) This means that the so-called date fields look like this : August 1, 2005, 9:09 am EDT October 13, 2004,...
12
by: Vincent Delporte | last post by:
Hello My site is hosted on a server in the US, hence set up to use the mm/dd/yyyy date format instead of the European dd/mm/yyyy. Also, MySQL stores dates as yyyy-mm-dd, so I need to convert...
6
by: marc | last post by:
hi im trying to convert Date() into a unix timestamp so i can stick the result into a mysql db, please help!
8
by: Tony B | last post by:
I have a string in a existing php script which is in the form "dd/mm/yyyy" and I need to convert it into a suitable format for mysql which is "yyyy-mm-dd" Is there a neat way of doing this in php ?
11
by: Keith Hughitt | last post by:
Hi, I am having a little trouble figuring out how to convert a python datetime to UTC. I have a UTC date (e.g. 2008-07-11 00:00:00). I would like to create a UTC date so that when I send it to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.