How to calculate the difference in days in php

藏色散人
Release: 2023-03-08 08:00:02
Original
4105 people have browsed it

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.

How to calculate the difference in days in php

#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);
Copy after login

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; }
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!