Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string is a valid DATE, TIME or DATETIME

Tags:

When I try to put a value into a DATE field which is invalid, MySQL seems to use 0000-00-00 instead. Is there a way I can do this "check" without updating a DATE field? And to do it from for example PHP?

Like, is there a way I can query the MySQL server and ask "Hey, is this DATE, TIME or DATETIME valid to you?"

Or is there maybe an even better way of doing it?

like image 770
Svish Avatar asked Apr 22 '10 11:04

Svish


People also ask

How do you validate if a string is a date?

Using the Date. One way to check if a string is date string with JavaScript is to use the Date. parse method. Date. parse returns a timestamp in milliseconds if the string is a valid date.

How check string is DateTime or not in C#?

TryParse() function to check if a particular string is a valid datetime not depending on any cultures. To my surprise , the function returns true for even strings like "1-1", "1/1" . etc.

How do you verify if a string is a date in Java?

DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy"); assertTrue(validator. isValid("02/28/2019")); assertFalse(validator. isValid("02/30/2019")); This was the most common solution before Java 8.

How do I check if a string is in mm dd yyyy?

The toISOString() method returns a string formatted as YYYY-MM-DDTHH:mm:ss. sssZ . If the ISO representation of the Date object starts with the provided string, we can conclude that the date string is valid and formatted as YYYY-MM-DD .


1 Answers

I use this syntax in my Application and it's work like charm!

SELECT DATE('2013-09-31') AS valid; 

My System is Win7x64 with PHP 5.5 and MySQL 5.6.16

like image 83
Abbas Avatar answered Sep 22 '22 22:09

Abbas