Php date and time comparison function

Source: Internet
Author: User
Tags date1 explode

$ Date1 = "2007-10-25 ";
$ Date2 = time ();
Echo "$ date1 compare to $ date2 ";

// 2007-10-25 compare to 1189772448

$ Date1 = "2007-10-25 ";
$ Date2 = date ("y-m-d", time ());
Echo "$ date1 compare to $ date2 ";


/*
You don't know how big the difference between the two dates is. If you want to know this information, it makes sense to convert it to the unix timestamp format. In this case, you have an integer and two dates. Poor computation is a very simple integer. Later, you can convert to minutes, hours, days, or seconds as long as you want.

To use this solution, we need to use the explode () function to help divide the data into smaller date strings. Then, we are all in the date in a separate variable factor, so we can use the mktime () function. This function will work with unix timestamp and return, so we can further calculate
*/

$ Date1 = "2007-10-25 ";
$ Date2 = time ();
$ Datearr = explode ("-", $ date1 );
$ Date1int = mktime (0, 0, $ datearr [1], $ datearr [2], $ datearr [0]);
Echo "$ date1int compare to $ date2 difference is". ($ date1int-$ date2 );

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.