本文章介绍了三个自定义函数,一个日期验证,一个时间验证,一个验证是否为时间和日期的,有需要的同学可以参考.
日期验证格式2011-12-12,代码如下:
- function is_date( $str )
- {
- $YEAR = "";
- $MONTH = "";
- $DAY = "";
- $len = strlen( $str );
- $offset = 0;
- $i = strpos( $str, "-", $offset );
- $YEAR = substr( $str, $offset, $i - $offset );
- $offset = $i + 1;
- if ( $len < $offset )
- {
- return FALSE;
- }
- if ( $i )
- {
- $i = strpos( $str, "-", $offset );
- $MONTH = substr( $str, $offset, $i - $offset );
- $offset = $i + 1;
- if ( $len < $offset )
- {
- return FALSE;
- }
- if ( $i )
- {
- $DAY = substr( $str, $offset, $len - $offset );
- }
- }
- if ( $YEAR == "" || $MONTH == "" || $DAY == "" )
- {
- return FALSE;
- }
- if ( !checkdate( intval( $MONTH ), intval( $DAY ), intval( $YEAR ) ) )
- {
- return FALSE;
- }
- return TRUE;
- }
时间验证格式11:12:32,代码如下:
- function is_time( $str )
- {
- $TEMP = "";
- $HOUR = "";
- $MIN = "";
- $SEC = "";
- $TEMP = strtok( $str, ":" );
- $HOUR = $TEMP;
- if ( $HOUR == "" || 24 <= $HOUR || $HOUR < 0 || !is_number( $HOUR ) )
- {
- return FALSE;
- }
- $TEMP = strtok( ":" );
- $MIN = $TEMP;
- if ( $MIN == "" || 60 <= $MIN || $MIN < 0 || !is_number( $MIN ) )
- {
- return FALSE;
- }
- $TEMP = strtok( ":" );
- $SEC = $TEMP;
- if ( $SEC == "" || 60 <= $SEC || $SEC < 0 || !is_number( $SEC ) )
- {
- return FALSE;
- }
- return TRUE;
- }
时间日期验证:2011-12-12 12:12:12,代码如下:
- function is_date_time( $DATE_TIME_STR )
- {
- if ( $DATE_TIME_STR == NULL || strlen( $DATE_TIME_STR ) == 0 )
- {
- return FALSE;
- }
- $DATE_TIME_ARRY = explode( " ", $DATE_TIME_STR );
- if ( is_date( $DATE_TIME_ARRY[0] ) && is_time( $DATE_TIME_ARRY[1] ) )
- {
- return TRUE;
- }
- return FALSE;
- }
|