Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > mysql date question
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 5th November 02:48
awilliam
External User
 
Posts: 1
Default mysql date question



I have a field in mysql as shown by describe contract;

| length_start | date | YES | | NULL
| |

Which stores it in the mysql format of YYYY-MM-DD. However, I need the
output of my select statement to show it in MM-DD-YYYY format. I can
select it to see the date in the field:

select length_start from contract where user_id = 1;
+--------------+
| length_start |
+--------------+
| 2006-01-12 |
+--------------+
1 row in set (0.00 sec)

so then I do my date_format() select statement, but it returns a NULL
value. Why?

select date_format('contract.length_start', '%m-%d-%Y') as length_start
from contract where user_id = 1;
+--------------+
| length_start |
+--------------+
| NULL |
+--------------+
1 row in set, 1 warning (0.00 sec)
  Reply With Quote


 


2 8th November 03:47
stuttle
External User
 
Posts: 1
Default mysql date question



This has nothing to do with PHP, but the first parameter to date_format
should not be in quotes.

-Stut

--
http://stut.net/
  Reply With Quote
3 8th November 03:47
jblanchard
External User
 
Posts: 1
Default mysql date question


[snip]
I have a field in mysql as shown by describe contract;

| length_start | date | YES | | NULL
| |

Which stores it in the mysql format of YYYY-MM-DD. However, I need the
output of my select statement to show it in MM-DD-YYYY format. I can
select it to see the date in the field:

select length_start from contract where user_id = 1;
+--------------+
| length_start |
+--------------+
| 2006-01-12 |
+--------------+
1 row in set (0.00 sec)

so then I do my date_format() select statement, but it returns a NULL
value. Why?

select date_format('contract.length_start', '%m-%d-%Y') as length_start
from contract where user_id = 1;
+--------------+
| length_start |
+--------------+
| NULL |
+--------------+
1 row in set, 1 warning (0.00 sec)
[/snip]


Actually this is more a question for the MySQL list.

Start first by taking the ticks or quotes off of the column;

select date_format(contract.length_start, '%m-%d-%Y') as length_start
from contract where user_id = 1;

You are essentially trying to turn that text string into a date, it will
not work.
  Reply With Quote
4 8th November 03:47
awilliam
External User
 
Posts: 1
Default mysql date question


nevermind, figure it out, had to take the ' ' away from
contract.length_start
  Reply With Quote
5 8th November 03:47
bastien_k
External User
 
Posts: 1
Default mysql date question


no need for quotes

select date_format(contract.length_start, '%m-%d-%Y') as length_start from contract where user_id = 1;

bastien

__________________________________________________ _______________
Discover new ways to stay in touch with Windows Live! Visit the City @ Live today!
http://getyourliveid.ca/?icid=LIVEIDENCA006
  Reply With Quote
6 8th November 03:47
sporc
External User
 
Posts: 1
Default mysql date question


Uhm, a shot in the dark - try:
select date_format(contract.length_start, '%m-%d-%Y') as length_start

HTH, cheers!
Silvio
  Reply With Quote
7 8th November 03:48
ceo
External User
 
Posts: 1
Default mysql date question


There is not PHP in this question.

But to save you subscribing/posting/unsubcribing to the MySQL list:

You put apostrophes on 'contract.length_start' which makes it a
literal DATE.

MySQL silently ignores such a stupid-looking date, and makes it NULL.

Take away the apostrophes on the FIELD NAME and all will be good.

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?
  Reply With Quote
Reply


Thread Tools
Display Modes




666