Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

MySQL Date Function.

Quote Reply
MySQL Date Function.
Hi,

I am trying to get MySQL to record a date using PHP. Anyway - Heres the code I am trying.

Code:
mysql_query ("INSERT INTO phpusers (users_id, firstname, lastname, email, joined)
VALUES ('NULL','$firstname', '$lastname', '$email', 'NULL')
");

Anyways - The code works, but the date turns out as '0000-00-00' all the time... Frown

Any tips? Do I change it from Null to something else? Thanks Smile

------------------
Michael Bray
Review your webhost or find a new one.
www.webhostarea.com
Links SQL User
------------------




Quote Reply
Re: MySQL Date Function. In reply to
The "null" for date causes nothing to be entered, so the value defaults to the empty default of 0000-00-00

Try assigning the field a value of the current date or time, rather than null.

------------------
POSTCARDS.COM -- Everything Postcards on the Internet www.postcards.com
LinkSQL FAQ: www.postcards.com/FAQ/LinkSQL/








Quote Reply
Re: MySQL Date Function. In reply to
Damn Smile

OK - I thought it was like the auto_increment for ID where I say Null anbd it automatically handles it.

If I am going to use code to get the date, why not just set the field as a varchar?

Thanks.

------------------
Michael Bray
Review your webhost or find a new one.
www.webhostarea.com
Links SQL User
------------------




Quote Reply
Re: MySQL Date Function. In reply to
By setting it to a "date" field, you gain access to a bunch of built in functions when you try to search/sort by date or time.

Quote Reply
Re: MySQL Date Function. In reply to
Thanks for including the url Michael, increased knowledge now to know, "F - month, textual, long; i.e. "January""

Have you read thru this thread where Alex provides some feedback on the date functions?
http://www.gossamer-threads.com/scripts/forum/resources/Forum9/HTML/000314.html

Not sure of course, but could provide some insight. Since the db is using the MySQL date defaults of 0000-00-00 - php may not be able to give the display you want within the db without changing the display for dates within the db itself. ???
Quote Reply
Re: MySQL Date Function. In reply to
Thanks All!

$today = date("Y-m-d");

Worked like a charm. Anyway - now I can keep working on my user database :P

------------------
Michael Bray
Review your webhost or find a new one.
www.webhostarea.com
Links SQL User
------------------




Quote Reply
Re: MySQL Date Function. In reply to
Michael, what is the "F" in your line?
$today = date("d-F-Y");
Quote Reply
Re: MySQL Date Function. In reply to
http://www.php.net/manual/function.date.php3

Its the day of the month... in text format which might be a problem :P

I'll move it to numeric format and see what happens.

------------------
Michael Bray
Review your webhost or find a new one.
www.webhostarea.com
Links SQL User
------------------




Quote Reply
Re: MySQL Date Function. In reply to
OK,

I tried this...

$today = date("dmY");

and it prints out 06032000 but in the MySQL database will only right 0000-00-00

Argh - Every other field works!

------------------
Michael Bray
Review your webhost or find a new one.
www.webhostarea.com
Links SQL User
------------------




Quote Reply
Re: MySQL Date Function. In reply to
I'd have to look at the perl docs, but what if you tried:

date("Y-m-d") instead of d-m-Y ??

Quote Reply
Re: MySQL Date Function. In reply to
Its starting to piss me off Smile

Code:
$today = date("d-F-Y");

mysql_query ("INSERT INTO phpusers (users_id, firstname, lastname, email, joined)
VALUES ('NULL','$firstname', '$lastname', '$email', '$today')
");
(its PHP)

Still makes it all 0's in the date, but everything else workds well.

If I Say

Code:
print ($today);

It prints the right day though Frown - Oh well, I'll find out whats wrong sooner or later.

------------------
Michael Bray
Review your webhost or find a new one.
www.webhostarea.com
Links SQL User
------------------




Quote Reply
Re: MySQL Date Function. In reply to
Thanks for posting what worked!

That's as important as all the possible solutions!



------------------
POSTCARDS.COM -- Everything Postcards on the Internet www.postcards.com
LinkSQL FAQ: www.postcards.com/FAQ/LinkSQL/








Quote Reply
Re: MySQL Date Function. In reply to
Not a problem Smile - After using the FAQ on your site a fair bit, I like it when the solution is shown!

------------------
Michael Bray
Review your webhost or find a new one.
www.webhostarea.com
Links SQL User
------------------




Quote Reply
Re: MySQL Date Function. In reply to
A little late in the thread, but you can also do:

Code:
INSERT INTO phpusers (users_id, firstname, lastname, email, joined)
VALUES ('NULL','$firstname', '$lastname', '$email', NOW())

the NOW() is a mysql date function that returns the current time.

Cheers,

Alex

Quote Reply
Re: MySQL Date Function. In reply to
Thanks,

If that returns the time, I guess I'll add a time field cause the one I did just puts the date there...



------------------
Michael Bray
Review your webhost or find a new one.
www.webhostarea.com
Links SQL User
------------------




Quote Reply
Re: MySQL Date Function. In reply to
NOW() is probably a DATETIME item, I don't have my manual here to check.



------------------
POSTCARDS.COM -- Everything Postcards on the Internet www.postcards.com
LinkSQL FAQ: www.postcards.com/FAQ/LinkSQL/