954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Input date to mysql with 0 in front

Hello

Today is the first of february and I noticed a bug in my coding.

I use the php date function to give me todays date in an european style: 010210

I then insert this date to a mysql table.

The problem is that the date after being inserted is listed as 10210..

This of course makes my other code go crazy..

How can I get the exact "number" into mysql? The column I'm inserting into is VARCHAR(100). Also tried TEXT but did not help..

jvestby
Newbie Poster
4 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Why do you use a TEXT column to store a DATE value ?

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Mysql date structure is YYYY-MM-DD. (I thought that was Euro-style!) It's not configurable. We use MM/DD/YYYY here in the USA, so we have to bend and twist the date into our format.

Anyway, you use the PHP functions strtotime() and strftime() to manipulate the date into whatever format you wish. (see PHP manual).

JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
 

Yes, you are all correct but since I'm a newbie at php and mysql I was just wondering if there is a way that you can store "010210" in mysql without having to rewrite my code. Sort of a quick fix...

jvestby
Newbie Poster
4 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

I don't know. Did you try int ?
I know that you can get a leading 0 with decimal using all decimal numbers in a format like decimal 6,6 ,but there is that pesky dot...
In the end, the best way is the right way.
How much re-writing could it be?
That's the beauty of this forum, you can ask a question BEFORE you jump in the deep end.

JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
 

Yeah, I guess I'll have to go the right way..;) Just a couple of hours of recoding..;)

Thanks anyway.

jvestby
Newbie Poster
4 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

not sure if you have it figured out yet or not but if not you could make that column a int(6) UNSIGNED ZEROFILL. This should then start your date with a lead 0 if it is not 6 ints long.

I have never used it like that before but i know it works when i am counting and want lead zeros.

ex. 1 would be 000001

njm5785
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You