A summary of the problem of PHP time calculation

Source: Internet
Author: User
Tags diff echo date

Recently learning PHP, a friend asked the time of the calculation, at this time I thought of the Delphi and MSSQL computing functions, they are very convenient to use, but check the PHP manual did not find a similar time calculation function, through the web-inspired and self test, or find a simple way to achieve

Specifically as follows:

1> as we know the start time, we need to add a time to get a result time, you can use the following code

$time 1 = ' 2008-10-1 12:30:30 ';

echo Date (' y-m-d h:i:s ', Strtotime ($time 1) +30*60);/note the case within quotation marks, the minute is I not M

Implementation result: 2008-10-01 13:00:30

2> if we want to calculate the difference of two times, you can use the following methods:

$time 1 = ' 2008-10-1 12:30:30 ';

$time 2 = ' 2008-10-1 13:45:30 ';

$diff = (Strtotime ($time 2)-strtotime ($time 1))/60;

echo $time 1 ' to '. $time 2 ' time difference '. $diff. ' Minutes ';

Results: 2008-10-1-12:30:30 to 2008-10-1 13:45:30 's jet lag is 75 minutes.

Summary: PHP time calculation is based on the second, grasp this rule can be converted into minutes, hours and so on, so that time calculation becomes very simple and easy.

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.