Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP detect if search is date or not

Tags:

date

php

I have a search box on my website...mysql database. I check currently if the keyword search is a category or just a sentence that matches the title. I would like to enhance that further by detecting if a date is the search keyword, then it will know that and SELECT * posts relevant to that date. How can I detect if it is a date or not? maybe a regular expression?

For your info, I am using the standard format of YYYY/MM/DD so e.g. 2012/07/21.

like image 888
sys_debug Avatar asked Jul 31 '12 17:07

sys_debug


2 Answers

I'd test it with strtotime().

$keyword;

$time = strtotime($keyword);

if ($time !== false) {
  // $keyword is a parsable date!
}
like image 138
Mike B Avatar answered Oct 18 '22 20:10

Mike B


You might want to look at the PHP function strtotime(), which returns a timestamp or FALSE if not a date...

like image 2
neokio Avatar answered Oct 18 '22 21:10

neokio