Validate the datetime value: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 41: Line 41:
== PHP appracoh ==
== PHP appracoh ==
Using PHP [https://www.php.net/manual/en/function.strtotime.php strtotime] function
Using PHP [https://www.php.net/manual/en/function.strtotime.php strtotime] function
== Excel approach ==
# Validate the total length of cell value is equal to 10 e.g. {{kbd | key=<nowiki>=IF(LEN(B2)<>10, TRUE, FALSE)</nowiki>}}
# Validate the positions of the {{kbd | key=<nowiki>-</nowiki>}} symbols
# Combined the above condition, the verify formula is: (If the cell value is well-formatted, the formula returns true)
<pre>
=NOT(
    IF(OR(
          ISERROR(FIND("-", B2, 1)),
          ISERROR(FIND("-", B2, 6)),
          IF(LEN(B2)<>10, TRUE, FALSE)
          ), TRUE, FALSE)
)
</pre>


== References ==
== References ==

Revision as of 15:44, 15 August 2020

Validate the value should be well-formated value e.g. year-month-day 2026-05-06

MySQL approach

SELECT `my_date_column`, STR_TO_DATE(`my_date_column`, '%Y-%m-%d')
FROM `my_table`
WHERE
STR_TO_DATE(`my_date_column`, '%Y-%m-%d') IS NULL;

Verify the value should be year/month/day format

SELECT `my_date_column`, STR_TO_DATE(`my_date_column`, '%Y/%m/%d')
FROM `my_table`
WHERE
STR_TO_DATE(`my_date_column`, '%Y/%m/%d') IS NULL;

Verify the value should be hour:minute:second format e.g. 13:52:06

SELECT `my_time_column`, STR_TO_DATE(`my_time_column`, '%H:%i:%S') 
FROM `my_table`
WHERE
STR_TO_DATE(`my_time_column`, '%H:%i:%S') IS NULL;

Verify the value should be year-month-day hour:minute:second format e.g. 2026-05-06 13:52:06

SELECT `my_time_column`, STR_TO_DATE(`my_time_column`, '%Y-%m-%d %H:%i:%S') 
FROM `my_table`
WHERE
STR_TO_DATE(`my_time_column`, '%Y-%m-%d %H:%i:%S') IS NULL;

PHP appracoh

Using PHP strtotime function

Excel approach

  1. Validate the total length of cell value is equal to 10 e.g. =IF(LEN(B2)<>10, TRUE, FALSE)
  2. Validate the positions of the - symbols
  3. Combined the above condition, the verify formula is: (If the cell value is well-formatted, the formula returns true)
=NOT(
     IF(OR(
           ISERROR(FIND("-", B2, 1)), 
           ISERROR(FIND("-", B2, 6)), 
           IF(LEN(B2)<>10, TRUE, FALSE)
           ), TRUE, FALSE)
 )

References