PHP Date Time comparison three functions _php tutorial

Source: Internet
Author: User
The article introduces three kinds of commonly used date time comparison format function, one is to the whole date, one is only to the time comparison, the last one is the professional comparison difference can be to seconds.

Date comparison as of 2011-11-11 2011-12-12

The code is as follows Copy Code
function Compare_date ($DATE 1, $DATE 2)
{
$STR = Strtok ($DATE 1, "-");
$YEAR 1 = $STR;
$STR = Strtok ("-");
$MON 1 = $STR;
$STR = Strtok ("-");
$DAY 1 = $STR;
$STR = Strtok ($DATE 2, "-");
$YEAR 2 = $STR;
$STR = Strtok ("-");
$MON 2 = $STR;
$STR = Strtok ("-");
$DAY 2 = $STR;
if ($YEAR 2 < $YEAR 1)
{
return 1;
}
if ($YEAR 1 < $YEAR 2)
{
return-1;
}
if ($MON 2 < $MON 1)
{
return 1;
}
if ($MON 1 < $MON 2)
{
return-1;
}
if ($DAY 2 < $DAY 1)
{
return 1;
}
if ($DAY 1 < $DAY 2)
{
return-1;
}
return 0;
}

Format: 12.00-13.11

code as follows copy code
function Compare_time ($TIME 1, $TIME 2)
{
$STR = Strtok ($TIME 1, ":");
$HOUR 1 = $STR;
$STR = Strtok (":");
$MIN 1 = $STR;
$STR = Strtok (":");
$SEC 1 = $STR;
$STR = Strtok ($TIME 2, ":");
$HOUR 2 = $STR;
$STR = Strtok (":");
$MIN 2 = $STR;
$STR = Strtok (":");
$SEC 2 = $STR;
if ($HOUR 2 < $HOUR 1)
{
return 1;
}
if ($HOUR 1 < $HOUR 2)
{
return-1;
}
if ($MIN 2 < $MIN 1)
{
return 1;
}
if ($MIN 1 < $MIN 2)
{
return-1;
}
if ($SEC 2 < $SEC 1)
{
return 1;
}
if ($SEC 1 < $SEC 2)
{
return-1;
}
return 0;
}

Format: 2011-11-12 1:6:25, 2011-12-13 1:2:35

code as follows copy code

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;
}

http://www.bkjia.com/PHPjc/631661.html www.bkjia.com true http://www.bkjia.com/PHPjc/631661.html techarticle The article introduces three kinds of commonly used date time comparison format function, one is to the whole date, one is only to the time comparison, the last one is the professional comparison difference can be to seconds. 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.