Summary of some time functions in PHP

Source: Internet
Author: User
: This article mainly introduces some time functions of PHP. For more information about PHP tutorials, see. In general, there will always be time functions. below are several custom time functions:

① Calculate the time difference based on the two timestamps

/** Time difference calculation function */function timediff ($ begin_time, $ end_time) {if ($ begin_time <$ end_time) {$ starttime = $ begin_time; $ endtime = $ end_time ;} else {$ starttime = $ end_time; $ endtime = $ begin_time;} // computing days $ timediff = $ endtime-$ starttime; $ 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; $ res = array ("day" => $ days, "hour" => $ hours, "min" => $ mins, "sec" => $ secs); return $ res ;}
② Calculate the number of days of difference based on the two timestamps ---- timediff is the previous function

Function dayTime ($ begin_time, $ end_time, $ controller = null) {date_default_timezone_set ('Asia/Shanghai'); // Set the time zone, otherwise, use the default $ timediff = timediff ($ begin_time, $ end_time ); $ totalTime = floatval ($ timediff ['SEC '] + ($ timediff ['min'] * 60) + ($ timediff ['hour'] * 3600) + ($ timediff ['day'] * 3600*24)/3600); if ($ controller = null) {echo round ($ totalTime, 2 ); // retain two decimal places} else {return round ($ totalTime, 2 );}}
③ Determine whether the same day is based on the two timestamps ---- timediff is the previous function.

/** Determine whether the same day of the Year */function sameDay ($ begin_time, $ end_time) {// input the timestamp if (date ("z", $ begin_time )! = Date ("z", $ end_time) {// return false on the same day of a year;} elseif (date ("z", $ begin_time) = date ("z", $ end_time) {// return true on the same day of the year ;}}

The above section describes some of the time functions of PHP, including some of the content, hope to be helpful to friends who are interested in the PHP Tutorial.

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.