PHP calculates the difference between two time periods (in seconds, hour, day, month, and year)

Source: Internet
Author: User
Tags date1

PHP calculates the difference between two time periods (in seconds, hour, day, month, and year)

Sample Code of the month difference between two time periods:

The Code is as follows:  

$ Yourdate = "2012-10-20 ";
$ Yourdate_unix = strtotime ($ yourdate );
Echo (date ("Y", $ yourdate_unix)-date ("Y") * 12 + (date ("m", $ yourdate_unix) -date ("m "));

Example 1

The Code is as follows:  

/*
* Calculate the month difference of two time periods.
* @ Param $ st start time $ et End Time (timestamp format)
* @ Return $ difference value returned by total
*/
Function getMonthNum ($ st, $ et)
{
$ S_m = date ('n', $ st );
$ E_m = date ('n', $ et );
$ S_y = date ('y', $ st );
$ E_y = date ('y', $ et );
$ Total = 13-$ s_m + ($ e_y-$ s_y-1) * 12 + $ e_m; // calculates the month difference.
Return $ total;
}

Example 2

The Code is as follows:  

<? Php
$ One = strtotime ('2017-05-08 07:02:40 '); // start time stamp
$ Tow = strtotime ('2017-12-25 00:00:00 '); // End Time Stamp
$ Cle = $ tow-$ one; // get the timestamp difference

/* This is just a prompt
Echo ceil ($ cle/60); // The total number of minutes
Echo ceil ($ cle/3600); // calculates the total number of hours
Echo ceil ($ cle/3600/24); // returns the total number of days
*/
/* Ceil () function, which is an integer in the next Method */
$ D = cell ($ cle/3600/24 );
$ H = cell ($ cle % (3600*24)/3600); // % obtain the remainder
$ M = cell ($ cle % (3600*24)/60 );

Echo "two time differences $ d days $ h hours $ m points"
?>

Example 3

The Code is as follows:  

<? PHP

/*
*
* Function: Calculate two dates in the YYYY-MM-DD format, a few days different
*
*/
Function getChaBetweenTwoDate ($ date1, $ date2 ){

$ Date_List_a1 = explode ("-", $ date1 );
$ Date_List_a2 = explode ("-", $ date2 );

$ D1 = mktime (0, 0, 0, $ Date_List_a1 [1], $ Date_List_a1 [2], $ Date_List_a1 [0]);

$ D2 = mktime (0, 0, 0, $ Date_List_a2 [1], $ Date_List_a2 [2], $ Date_List_a2 [0]);

$ Days = round ($ d1-$ d2)/3600/24 );

Return $ Days;
}

Echo getChaBetweenTwoDate ('2017-08-11 ', '2017-08-16 ');
Echo "<br> ";
Echo getChaBetweenTwoDate ('2017-08-16 ', '2017-08-11 ');
?>

Example 4

The Code is as follows:  

<? Php
$ Startdate = "11:40:00 ″;
$ Enddate = "2012-12-12 11:45:09 ″;
$ Date = floor (strtotime ($ enddate)-strtotime ($ startdate)/86400 );
$ Hour = floor (strtotime ($ enddate)-strtotime ($ startdate) % 86400/3600 );
$ Minute = floor (strtotime ($ enddate)-strtotime ($ startdate) % 86400/60 );
$ Second = floor (strtotime ($ enddate)-strtotime ($ startdate) % 86400% 60 );
Echo $ date. "day <br> ";
Echo $ hour. "hour <br> ";
Echo $ minute. "minute <br> ";
Echo $ second. "seconds <br> ";
?>

Example 4 is my favorite one that can be calculated to days, hours, And seconds. Of course, you still need to use it as needed.

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.