How to calculate the time difference in PHP _php tutorial

Source: Internet
Author: User

How to calculate the time difference in PHP


There are several ways to calculate the time difference in PHP, and it is sometimes a hassle to calculate the difference in PHP! But as long as you have the use of the date-times function, these things become simpler:

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

(1) It's easy if you have a database! If MSSQL can use a trigger! Use a function DateDiff () that calculates the date difference.

If MySQL is the result of the calculation of the difference between the two date fields, the results are saved in another numeric field! Time to call!

(2) If you don't have a database, you have to use PHP's time-date function completely! The following are the main explanations:

Example: Calculate 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 to get;

If the parameter in Mktime () is the default, it means that the current date is used, so that the number of days from the borrowing date is calculated.

http://www.bkjia.com/PHPjc/847197.html www.bkjia.com true http://www.bkjia.com/PHPjc/847197.html techarticle how to calculate the time difference in PHP several methods of calculating the time difference in PHP, the calculation of the time difference in PHP is sometimes a hassle! But as long as you've mastered the use of datetime functions, these ...

  • 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.