Home > Article > Backend Development > How to calculate the difference in days in php
php method to calculate the difference in days: first create a PHP sample file; then define two time points; then use strtotime to format the time into a timestamp; finally, directly output "$days" to obtain The difference in days.
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
php calculates the number of days difference between two dates, processing time interval
Example: Want to get the number of days difference between 2016-04-10 and 2016-06-15.
$begin_date = strtotime('2016-04-10'); $end_date = strtotime('2016-06-15'); $days = round(($end_date - $begin_date) / 3600 / 24);
Strtotime has been used to format the time into a timestamp, and $days is output directly to obtain the number of days difference.
Attached is also a method for handling time intervals. (Recommended: "PHP Video Tutorial")
/* * 处理时间问题 */ public function get_time($from,$to){ if($to > $from){ $miao = $to - $from; }else{ $miao = $from - $to; } $hour = floor($miao/3600); $minute = floor(($miao-$hour*3600)/60); $second = $miao - $hour * 3600 - $minute * 60 ; $str = ''; if($hour > 0){ $str .= $hour.'时'; } if($minute > 0){ $str .= $minute.'分'; } if($second){ $str .= $second.'秒'; } return $str; }
The above is the detailed content of How to calculate the difference in days in php. For more information, please follow other related articles on the PHP Chinese website!