How to calculate the time difference in PHP

Source: Internet
Author: User
Tags time and date

Several ways to calculate the time difference in PHP, the calculation of the time difference in PHP is sometimes a hassle! But as long as you have the use of the date function, that's a simple thing to do:

A simple example is calculating the number of days to borrow, which requires PHP to calculate on a daily basis, and here's a few ways to implement this date calculation:

(1) If there is a database is very easy! If MSSQL can use triggers! A function that calculates the date difference specifically DATEDIFF ()!

In the case of MySQL, use the two-date field's difference calculation to save the result in another numeric field! Time to call!

(2) If there is no database, then you have to completely use the PHP time and date function! The following main description:

Example: Calculates the number of days from May 3, 1998 to 1999-6-5:

$enddate =mktime ("0", "0", "0", "6", "5", "1999");

The resulting value is the total number of seconds from 1970-1-1 to the parameter time: is an integer. So

The following code is so much more:

$days =round (($enddate-$startdate)/3600/24);

Echo $days;

Days for the day;

If the parameter in Mktime () defaults, that means the current date is used, so that you can calculate the number of days from the date the book was borrowed.

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.