In Action

Description

This is a simple function that can be used to check a SQL Server date. For example, you have an application that accepts date input from a user to use as part of a SQL statement or stored procedure.

Php in Action


forloop



Toycode

Php Code

Just cut-n-paste to reuse.

     
<?php
$str = '2009-12-31';
$DateStr_yyyy_mm_dd = preg_split('/[-]/', $str, -1, 
                                 PREG_SPLIT_OFFSET_CAPTURE);
 echo "Year is " . $DateStr_yyyy_mm_dd[0][0] . "
"; echo "Month is " . $DateStr_yyyy_mm_dd[1][0] . "
"; echo "Day is " . $DateStr_yyyy_mm_dd[2][0] . "
"; $ret = validSQLServerDate($str); echo "am I a valid formatted date? " . $ret; function validSQLServerDate($dte) { $status = 'y'; //assume it is good to begin with $DateStr = preg_split('/[-]/', $dte, -1, PREG_SPLIT_OFFSET_CAPTURE); if (count($DateStr[1][0]) != 2) { $status = 'n'; } if (count($DateStr[1][0]) != 2) { $status = 'n'; } if (count($DateStr[0][0]) != 4) { $status = 'n'; } $validdate = checkdate ( $DateStr[1][0] , $DateStr[2][0] , $DateStr[0][0] ); if ($validdate) { $status = 'y'; } else { $status = 'n'; } return $status; } ?>