开发者

Searching Database by Arbitrary Date in PHP

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 开发者_如何学Goall 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?


Try strtotime It is VERY good at guessing dates correctly.

  • "Last week"
  • "Next Week"
  • "+15 days"
  • "Last tuesday"
  • "October 31st"
  • etc.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜