Searching Database by Arbitrary Date in PHP
Posted
by jverdi
on Stack Overflow
See other posts from Stack Overflow
or by jverdi
Published on 2010-03-21T22:42:38Z
Indexed on
2010/03/21
22:51 UTC
Read the original article
Hit count: 323
Suppose you have a messaging system built in PHP with a MySQL database backend, and you would like to support searching for messages using arbitrary date strings.
The database includes a messages table, with a 'date_created' field represented as a datetime.
Examples of the arbitrary date strings that would be accepted by the user should mirror those accepted by strtotime.
For the following examples, searches performed on March 21, 2010:
"January 26, 2009" would return all messages between 2009-01-26 00:00:00 and 2009-01-27 00:00:00
"March 8" would return all messages between 2010-03-08 00:00:00 and 2010-01-26 00:00:00
"Last week" would return all messages between 2010-03-14 00:00:00 and 2010-03-21 018:25:00
"2008" would return all messages between 2008-01-01 00:00:00 and 2008-12-31 00:00:00
I began working with date_parse, but the number of variables grew quickly. I wonder if I am re-inventing the wheel.
Does anyone have a suggestion that would work either as a general solution or one that would capture most of the possible input strings?
© Stack Overflow or respective owner