A case of PHP time format check function

Source: Internet
Author: User
This article describes a PHP implementation of the time format check of a function to check whether a given time is well-formed. Please refer to the friends who are in need.

The following code can be used to verify whether the time that the user entered is compliant. Example:

 
      ";    $str = "2012-02-30 12:31:22";    echo $str. "-";    echo isdatetime ($STR)? "TRUE": "FALSE";    echo "\ n";    $str = "2012-02-10 12:31:22";    echo $str. "-";    echo isdatetime ($STR)? "TRUE": "FALSE";    echo "\ n";    $str = "2012-02-10";    echo $str. "-";    Echo Isdatetime ($str, "YMD")? "TRUE": "FALSE";    echo "\ n";    $str = "2012-02-10";    echo $str. "-";    Echo Isdatetime ($str, "y-m-d")? "TRUE": "FALSE";? >

Output Result:

2012-02-30 12:31:22-false 2012-02-10 12:31:22-true 2012-02-10-false 2012-02-10-true

Code Description: Convert the incoming time into a timestamp with strtotime, then use the DATE function to convert to the specified format, if the converted string is the same as the one passed in, the format is correct.

  • 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.