Use php to calculate the difference between two dates

Source: Internet
Author: User
Welcome to the Linux community forum, and interact with 2 million technical staff to enter the number of days for calculation of the difference between the given two dates using php: the calculation method is not just described below, but some of the more common methods: to calculate the difference between And :? Php $ startdatestrtotime ("2009-12-09"); $ enddatestrtoti

Welcome to the Linux community forum, interact with 2 million technical staff> enter the number of days for calculating the difference between the given two dates in php: the calculation method is not only described below, but some of the more common methods: to calculate the difference between And :? Php $ startdate = strtotime ("2009-12-09"); $ enddate = strtoti

Welcome to the Linux community forum and interact with 2 million technicians>

Use php to calculate the number of days for the given two dates:

The calculation method is not only described below, but also a common method:

To calculate the difference between and:

$ Startdate = strtotime ("2009-12-09 ");

$ Enddate = strtotime ("2009-12-05 ");

The above php time and date function strtotime has converted the string date into a timestamp, so that we only need to subtract the two values and then convert the second to the day. The comparison is simple, as shown below:

$ Days = maid ($ enddate-$ startdate)/3600/24 );

Echo $ days; // days indicates the number of days;

?>

The following describes another method:

The size of the two dates is determined above, and the program code for judging the birthday is as follows. The resulting $ n is the number of days of the birthday.

$ Birthday = "birthday ";

$ Birthday = preg_replace ('/\ d +/', Date ('y'), $ birthday, 1 );

$ D = 60*60*24;

$ N = floor (strtotime ($ birthday)-time ()/$ d );

$ N = $ n + 1;

In addition, if we compare the current time, we can use the time () function to obtain the current timestamp.

In the second case, there is a database, which is relatively easier! You can use a trigger for MSSQL! You can use the datediff () function to calculate the date difference!

If it is MYSQL, use the timestamp value of the two date fields. After calculation, you can get the number of days of difference. The method is similar to the above Code.

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.