Php calculates the difference between two timestamps by day, hour, minute, and second.

Source: Internet
Author: User

Php calculates the difference between two timestamps by day, hour, minute, and second.

Php calculates the difference between two timestamps by day, hour, minute, and second.

/*** Calculate the difference between two timestamps by day, hour, and minute * @ param $ unixTime_1 start timestamp * @ param $ unixTime_2 end timestamp * @ return array */function timeDiff ($ unixTime_1, $ unixTime_2) {$ timediff = abs ($ unixTime_2-$ unixTime_1); // computing days $ days = intval ($ timediff/86400 ); // calculated hours $ remain = $ timediff % 86400; $ hours = intval ($ remain/3600); // calculates the number of minutes $ remain = $ remain % 3600; $ mins = intval ($ remain/60); // calculates the number of seconds $ secs = $ remain % 60; return ['day' => $ days, 'hour' => $ hours, 'Min' => $ mins, 'sec '=> $ secs];} // Array (// [day] => 1 // [hour] => 0 // [min] => 3 // [sec] => 31 //)
/*** Calculate the difference between the two timestamps by day, hour, and minute * @ param $ date1 start time * @ param $ date2 end time * @ return array */function diffDate ($ date1, $ date2) {$ datetime1 = new \ DateTime ($ date1); $ datetime2 = new \ DateTime ($ date2); $ interval = $ datetime1-> diff ($ datetime2 ); $ time ['Year'] = $ interval-> format ('% y'); $ time ['month'] = $ interval-> format (' % m '); $ time ['day'] = $ interval-> format ('% D'); $ time ['hour'] = $ interval-> format ('% H '); $ time ['Min'] = $ interval-> format ('% I'); $ time ['sec '] = $ interval-> format (' % s '); $ time ['days '] = $ interval-> format (' % A'); // returns $ time for the total days of two time differences ;} // Array (// [year] => 00 // [month] => 1 // [day] => 2 // [hour] => 00 // [min] => 3 // [sec] => 31 // [days] => 33 //)

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.