473,795 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1915
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
13150
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
2243
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 records returned when I execute: $sql = "select * from tbl where ExpiryDate > " . date("Y-m-d"); mysql_query($sql);
5
3497
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, because the dates must have quotes on each side. I just don't know how make the dates right. Well I'll just show you the code and some insert statements it generates. Could anyone please help me?
0
1475
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 myself but now I want to move to textpattern (http://www.textpattern.com/). Using a mySQL tool called NaviCat, I can easily create queries regarding porting ID, text data etc. from my old DB into the textpattern DB (using INNER JOINS it seems). But...
3
23824
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:28 pm EDT This makes them essentially useless for sorting and logical evaluation. Understandbly, the MySQL date functions don't seem able to convert the field to a "real" date field. This yields a blank (or maybe null) "realdate" :
12
48231
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 dd/mm/yyyy to that format. I'm a newbie, and didn't find the answer through Google :-/ Do I need to call another function in addition to strtotime() to make
6
34102
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
3059
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
3911
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 MySQL (which treats all dates at local dates by default), it will already have incorporated the proper UTC offset. I've tried looking through the docs http://python.active-venture.com/lib/datetime-datetime.html), but have not had any luck.
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.