Implementation code about the PHP time function encapsulation

Source: Internet
Author: User
This article to share to everyone is about the PHP time function encapsulation Implementation code, the content is very valuable, I hope to help the need for small partners.

One, two days of difference between dates

function DateDiff ($time 1, $time 2, $absolute = False) {    $time 1 = ($temp = strtotime ($time 1))? $temp: $time 1);    $time 2 = ($temp = Strtotime ($time 2))? $temp: $time 2);    $temp = (Strtotime (date (' Ymd ', $time 1))-Strtotime (Date (' Ymd ', $time 2))/86400;    Return $absolute? ABS ($temp): $temp;}

PHP comes with a function date_diff need to pass in the DateTime object, more troublesome. The top method returns the number of days between two time/timestamp differences. The idea is: if it is time, turn the time into a timestamp, format it to 0 points of the day and then convert back to timestamp, subtract divided by 86400. Of course, if the method is just a two-timestamp difference in number of days, the first line of the second line of the conversion timestamp code can be removed. 2018-01-01 23:59:59 and 2018-01-02 00:00:00 difference 1 days. 2018-01-01 00:00:00 and 2018-01-02 23:59:59 also differ by 1 days.

Second, current time milliseconds

function msec () {    return sprintf ('%.0f ', Microtime (True) * 1000);}

PHP does not have a function to directly return milliseconds, here is a microsecond format for the production of microseconds.

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.