473,387 Members | 1,453 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.

To Get a Date into MySql: there must be a better way.

MySql seems to only accept dates as 'yyyy-mm-dd'. How do I do that when
the user might input dd/mm/yy, or d.m.yyyy (I'm in Europe)? I know I can
do sscanf, or explode to rebuild the date string - but there must be a
better way. Is there a FAQ somwhere?

tks.

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Mar 11 '06 #1
8 1673
hugh webster wrote:
MySql seems to only accept dates as 'yyyy-mm-dd'. How do I do that when
the user might input dd/mm/yy, or d.m.yyyy (I'm in Europe)? I know I
can do sscanf, or explode to rebuild the date string - but there must
be a better way. Is there a FAQ somwhere?

tks.


Since you're asking about MySQL, I suggest your try a MySQl newsgroup -
such as comp.databases.mysql.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 11 '06 #2
In article <op.s59iljj9fdqzms@mercury>,
"hugh webster" <hw******@bluewin.ch> wrote:
MySql seems to only accept dates as 'yyyy-mm-dd'. How do I do that when
the user might input dd/mm/yy, or d.m.yyyy (I'm in Europe)? I know I can
do sscanf, or explode to rebuild the date string - but there must be a
better way. Is there a FAQ somwhere?


Well, there is a function - strtotime(), which will take plain english
and try to turn it into a timestamp.

For instance:

print date("Y-m-d", strtotime("Next tuesday"));

Will print:

2006-03-14

So, this won't really help with date formats like "2.7.2006", but it's
a base to build a more powerful "string to date" function on. You may
need to take your usual date formats and convert them into something
strtotime() understands. I've built a function that will turn "next
monday at 18" into "2006-03-13 18:00" and so on, by tweaking natural
english into time formats strtotime() understands.

For your specific examples, something like this could work:

# if it's in format 2.3.2006
if (preg_match("/^(\d{1,2})\.(\d{1,2})\.(\d{4})$/", $date, $m)){
$newdate = sprintf("%d-%02d-%02d", $m[3], $m[2], $m[1]);
}

# if it's in format 2/3/2006
if (preg_match("!^(\d{1,2})/(\d{1,2})/(\d{4})$!", $date, $m)){
$newdate = sprintf("%d-%02d-%02d", $m[3], $m[2], $m[1]);
}
The above will convert 2.3.2006 and 2/3/2006 to 2006-03-02.

Another option is to change the date format in MySQL, but 2006-01-01
is ISO standard, so I'd recommend working on the other end. That's
what I do.


--
Sandman[.net]
Mar 11 '06 #3
Take a look at http://www.tonymarston.net/php-mysql/dateclass.html

--
Tony Marston
http://www.tonymarston.net

"hugh webster" <hw******@bluewin.ch> wrote in message
news:op.s59iljj9fdqzms@mercury...
MySql seems to only accept dates as 'yyyy-mm-dd'. How do I do that when
the user might input dd/mm/yy, or d.m.yyyy (I'm in Europe)? I know I can
do sscanf, or explode to rebuild the date string - but there must be a
better way. Is there a FAQ somwhere?

tks.

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Mar 11 '06 #4
Sandman wrote:
In article <op.s59iljj9fdqzms@mercury>,
"hugh webster" <hw******@bluewin.ch> wrote:
MySql seems to only accept dates as 'yyyy-mm-dd'. How do I do that when
the user might input dd/mm/yy, or d.m.yyyy (I'm in Europe)? I know I can
do sscanf, or explode to rebuild the date string - but there must be a
better way. Is there a FAQ somwhere?


Well, there is a function - strtotime(), which will take plain english
and try to turn it into a timestamp.


Which is very Americanised, and does not have any mechanism for providing a
hint to the underlying code.

Best solution is not to allow users to enter a date into a text field. An
ugly solution is to provide dropdowns for day number, month name and year.
I think the best solution is to use something like Mihai Bazon's jscalendar
(which was one of the main contributing factors to me writing PfP Studio).

HTH

C.
Mar 11 '06 #5
Hugh,

The best solution I have found is to use a javascript calendar to assign
the value to the date field. It's easy for the users, and ensures you
get the date in the format you want. The other option is to have a
month, day, and year dropdown for them to select from.

It's a bit of extra work, but only the first time. If you write the code
in a reusable manner, it will save you countless hours down the road. In
fact, there are several javascript calendars out there which may be
ideal for your situation.

There was mention of using strtotime(), which could work. It basically
returns either a timestamp or false. However, the javascript method
would give you more consistent results, IMO.

Scott

hugh webster wrote:
MySql seems to only accept dates as 'yyyy-mm-dd'. How do I do that when
the user might input dd/mm/yy, or d.m.yyyy (I'm in Europe)? I know I
can do sscanf, or explode to rebuild the date string - but there must
be a better way. Is there a FAQ somwhere?

tks.

Mar 12 '06 #6
In article <fH***************@newsfe3-gui.ntli.net>,
Colin McKinnon
<co**********************@ntlworld.deletemeunlessU RaBot.com> wrote:
Sandman wrote:
In article <op.s59iljj9fdqzms@mercury>,
"hugh webster" <hw******@bluewin.ch> wrote:
MySql seems to only accept dates as 'yyyy-mm-dd'. How do I do that when
the user might input dd/mm/yy, or d.m.yyyy (I'm in Europe)? I know I can
do sscanf, or explode to rebuild the date string - but there must be a
better way. Is there a FAQ somwhere?


Well, there is a function - strtotime(), which will take plain english
and try to turn it into a timestamp.


Which is very Americanised, and does not have any mechanism for providing a
hint to the underlying code.

Best solution is not to allow users to enter a date into a text field. An
ugly solution is to provide dropdowns for day number, month name and year.


I don't know why though. I am using strtotime() for swedish dates. I
just have to have a set of translations for it. In my CMS system,
users enter dates in a text field, and the system warns you if it
can't make a real date out of it.

--
Sandman[.net]
Mar 12 '06 #7
I've had a lot of success (and some serious "wow, that's cool"
reactions from users and execs) with a combination of strtotime and
active reponse to the user. I don't know what kind of target platform
you're looking at, but if you're able to expect a fairly recent browser
of your users, try using plain-text date fields with a JavaScript event
that makes an XmlHttpRequest back to your server, to a script (only
needs to be about 5 lines long) that returns the strtotime() of the
input text. It then replaces what the user typed into the date field
with the response from the server. So, for example, they type "next
tuesday", hit tab, the field fills out the full date and time, and they
can look at it and see that it's correctly interpretted their
plain-english date.

Also, as far as storing dates in MySQL itself - I've gotten in the
(admittedly somewhat odd) habit of storing all dates in the DB as plain
integer columns holding a UNIX timestamp. It may seem odd, but it
works, and it's what I'm used to, because I learned PHP/MySQL from
modding phpBB.

Hope that helps!!

Mar 13 '06 #8
In article <11**********************@i39g2000cwa.googlegroups .com>,
"ad**********@gmail.com" <ad**********@gmail.com> wrote:
I've had a lot of success (and some serious "wow, that's cool"
reactions from users and execs) with a combination of strtotime and
active reponse to the user. I don't know what kind of target platform
you're looking at, but if you're able to expect a fairly recent browser
of your users, try using plain-text date fields with a JavaScript event
that makes an XmlHttpRequest back to your server, to a script (only
needs to be about 5 lines long) that returns the strtotime() of the
input text. It then replaces what the user typed into the date field
with the response from the server. So, for example, they type "next
tuesday", hit tab, the field fills out the full date and time, and they
can look at it and see that it's correctly interpretted their
plain-english date.
That's exactly how I do it.
Also, as far as storing dates in MySQL itself - I've gotten in the
(admittedly somewhat odd) habit of storing all dates in the DB as plain
integer columns holding a UNIX timestamp. It may seem odd, but it
works, and it's what I'm used to, because I learned PHP/MySQL from
modding phpBB.


But it's a killer for complicated date queries on huge tables.


--
Sandman[.net]
Mar 13 '06 #9

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

Similar topics

5
by: Dominique Javet | last post by:
Hello, I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this "newbie" question... I've found no answer in the forum ... I've a date problem with my formular. In my mysql DB...
10
by: Bob Bedford | last post by:
I've two input fields in wich the user enter dates. I've put something like "enter date here (dd/mm/yyyy) but most of the user don't enter date in correct format. So I'd like to be able to get...
3
by: Stefan van Roosmalen | last post by:
Hi there, I have developed several applications in Delphi. I used Paradox for my data storage. Now, I want to convert these databases to MySql. So that why I am looking for a way to convert the...
4
by: MyOracle | last post by:
Hi everybody, I just curious about change date(0000-00-00) to date (00-00-0000) in mysql.Can anyone tell me about that. Thanks. izmanhaidi.
2
by: yasaswi | last post by:
I have date values in mm/dd/yy format in a text file. How can use the LOAD command or mysqlimport command and load those date values. Currently I just load the date values as text into MySQL tables...
2
by: Rob | last post by:
Hello all I have a field (called active) of type DATE and I want to add a default value. The default value must be the current date. Is this possible to do this in the table create statement? ...
1
by: Rotsj | last post by:
Hi, i'm using visual foxpro 9 with a mysql 5 database, for direct access to my database i use navicat. In visual foxpro i've set my date format to dd-mm-yyyy, also i did this in navicat. However...
10
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There is one...
3
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.