Comparison of date and time in php three functions _ PHP Tutorial

Source: Internet
Author: User
Tags date1 strtok
The date and time comparison functions in php. This article introduces three commonly used date and time comparison functions, one is for the whole date, the other is for the time comparison only, and the last is for the professional comparison time difference to seconds. The date comparison document introduces three commonly used date and time comparison formats. one is for the whole date, the other is for the time comparison only, and the last is for the professional comparison time difference to seconds.

The date comparison is shown in 2011-11-11 2011-12-12.

The code is as follows:
Function compare_date ($ DATE1, $ DATE2)
{
$ STR = strtok ($ DATE1 ,"-");
$ YEAR1 = $ STR;
$ STR = strtok ("-");
$ MON1 = $ STR;
$ STR = strtok ("-");
$ DAY1 = $ STR;
$ STR = strtok ($ DATE2 ,"-");
$ YEAR2 = $ STR;
$ STR = strtok ("-");
$ MON2 = $ STR;
$ STR = strtok ("-");
$ DAY2 = $ STR;
If ($ YEAR2 <$ YEAR1)
{
Return 1;
}
If ($ YEAR1 <$ YEAR2)
{
Return-1;
}
If ($ MON2 <$ MON1)
{
Return 1;
}
If ($ MON1 <$ MON2)
{
Return-1;
}
If ($ DAY2 <$ DAY1)
{
Return 1;
}
If ($ DAY1 <$ DAY2)
{
Return-1;
}
Return 0;
}

Format: 12.00-13.11

The code is as follows:
Function compare_time ($ TIME1, $ TIME2)
{
$ STR = strtok ($ TIME1 ,":");
$ HOUR1 = $ STR;
$ STR = strtok (":");
$ MIN1 = $ STR;
$ STR = strtok (":");
$ SEC1 = $ STR;
$ STR = strtok ($ TIME2 ,":");
$ HOUR2 = $ STR;
$ STR = strtok (":");
$ MIN2 = $ STR;
$ STR = strtok (":");
$ SEC2 = $ STR;
If ($ HOUR2 <$ HOUR1)
{
Return 1;
}
If ($ HOUR1 <$ HOUR2)
{
Return-1;
}
If ($ MIN2 <$ MIN1)
{
Return 1;
}
If ($ MIN1 <$ MIN2)
{
Return-1;
}
If ($ SEC2 <$ SEC1)
{
Return 1;
}
If ($ SEC1 <$ SEC2)
{
Return-1;
}
Return 0;
}

Format :,

The code is as follows:

Function compare_date_time ($ DATE_TIME1, $ DATE_TIME2)
{
If ($ DATE_TIME1 = NULL | strlen ($ DATE_TIME1) = 0 | $ DATE_TIME2 = NULL | strlen ($ DATE_TIME2) = 0)
{
Return-1;
}
$ DATE_TIME1_ARRY = explode ("", $ DATE_TIME1 );
$ DATE_TIME2_ARRY = explode ("", $ DATE_TIME2 );
If (compare_date ($ DATE_TIME1_ARRY [0], $ DATE_TIME2_ARRY [0]) = 1)
{
Return 1;
}
If (compare_date ($ DATE_TIME1_ARRY [0], $ DATE_TIME2_ARRY [0]) = 0)
{
If (compare_time ($ DATE_TIME1_ARRY [1], $ DATE_TIME2_ARRY [1]) = 1)
{
Return 1;
}
If (compare_time ($ DATE_TIME1_ARRY [1], $ DATE_TIME2_ARRY [1]) = 0)
{
Return 0;
}
Return-1;
}
Return-1;
}

Bytes. Date comparison...

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.