PHP detects whether a search is given or not.

I have a search box on my site ... mysql database. I am currently checking if the keyword search is a category or just a sentence that matches the name. I would like to improve this by indicating if the date is a search keyword, then he will know this and SELECT * messages related to that date. How to determine if it is a date or not? perhaps regex?

For your information, I use the standard format YYYY / MM / DD, for example, 2012/07/21.

+7
source share
3 answers

I would check it with strtotime() .

 $keyword; $time = strtotime($keyword); if ($time !== false) { // $keyword is a parsable date! } 
+7
source

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

+2
source

strtotime does not work for me, because strings and integers are dates. The following code is a much better solution.

 $value_date_array = date_parse($value); if(checkdate($value_date_array['month'], $value_date_array['day'], $value_date_array['year'])) 
+1
source

All Articles