Re: Date Question >We have a form where the user selects a date from a calendar, the date
>is in the format May 23, 2008.
Mysql doesn't do selecting from a calendar, so presumably this is done
from something else, say, using HTML. There is no necessary reason why
it has to return the month as "May" just because that is the way it
is presented to the user.
If you have that date in the format above as datestr, then
str_to_date(datestr, '%M %d, %Y')
will give you a MySQL date for that date.
>The date in the datebase is part of a filename (varchar column) and is
>in the format 05212008.
If you get the date into a MySQL date form, then the filename you
want is date_format(thedate, '%m%d%Y').
>What we need to do is get the starting and ending date of the week for
>the date selected in the form: May 23, 2008, and then get the records
>from the database where the dates fall within that range.
How do you define a week? Does it start on Sunday, Monday, or
something else? You may think the answer to that question is
obvious, but in other countries that same answer is obviously
strange. (MySQL likes to use its date type for dates, so don't
fight it and use them at least as an intermediate form for calculations,
and preferably also for storage. You can use date_format and
str_to_date to change formats for communicating with users.)
If you have a date thedate, then:
subdate(thedate, (weekday(thedate)+1) % 7)
is the first Sunday on or before thedate. To get the Saturday after
that, add 6 days with adddate() to the beginning number above.
The idea here: figure out how many days you are into the week, and
subtract that to get to the beginning of the week. weekday() returns
0 for Monday. If you want weeks that begin with Monday, leave out
the +1 above in the calculation of the first day of the week.
>Parsing the filename of the date: ABCD.05212008 is not a problem, how
>to convert the formats, find the starting and ending dates is....... |