Time and date validation function in PHP

Source: Internet
Author: User
Tags strlen strtok time and date

Date validation Format 2011-12-12

The code is as follows Copy Code
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;
}

Time Verification 11:12:32

The code is as follows Copy Code
function Is_time ($STR)
{
$TEMP = "";
$HOUR = "";
$MIN = "";
$SEC = "";
$TEMP = Strtok ($str, ":");
$HOUR = $TEMP;
if ($HOUR = = "" | | <= $HOUR | | $HOUR < 0 | | !is_number ($HOUR))
{
return FALSE;
}
$TEMP = Strtok (":");
$MIN = $TEMP;
if ($MIN = = "" | | <= $MIN | | $MIN < 0 | | !is_number ($MIN))
{
return FALSE;
}
$TEMP = Strtok (":");
$SEC = $TEMP;
if ($SEC = = "" | | <= $SEC | | $SEC < 0 | | !is_number ($SEC))
{
return FALSE;
}
return TRUE;
}

Time-Date Validation: 2011-12-12 12:12:12

The code is as follows Copy Code

function is _date_time ($DATE _time_str)
{
        if ($DATE _time_str = = NULL | | strlen ( $DATE _time_str) = = 0)
        {
      & nbsp;         return FALSE;
       }
        $DATE _time_arry = Explode ("", $DATE _time_str);
        if (is_date ($DATE _time_arry[0]) && is_time ($DATE _time_arry[ 1])
        {
         & nbsp;      return TRUE;
       }
        return FALSE;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.