: 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.