PHP date check similar to ASP isDate () for user-dated dates, times or dates / times

I am converting an ASP application to PHP and have difficulty deciphering date check functionality.

In ASP, I have an isDate () function that allows me to check the date regardless of its format. Therefore, if someone enters any of the following values, isDate () returns true:

1/1/2012 January 1, 2012 Jan 12, 2010 1:00 pm 1/1/2012 1:32 pm 

The advantage is that I can let the user enter data and time in whatever format they choose. After that, with minimal effort, I can update the database without doing any further processing (provided that true is returned). For instance:

 UPDATE my_table SET date_field = @date_var WHERE my_id = @id_var [note: SQL Server 2008] 

With PHP, I find that checking date / time is more complicated. Ive looked at the various questions posted here, as well as the documentation, and there seems to be no easy way to easily confirm a date, time, or a combination of date and time.

I am wondering if there are classes available to work with grunts. Something that will work as isDate () and send me true / false based on the date / time / date and time entered by the user.

Thanks.

+4
source share
2 answers

You can check the return value of strtotime() to see if date parsing can:

 if(strtotime($date) !== false) { // valid date/time } 

It also normalizes all dates, storing them as the return value of strtotime() .

+13
source

Perhaps you could use strtotime ()?

+1
source

Source: https://habr.com/ru/post/1388160/


All Articles