How to get the number of days difference between a given date in php

墨辰丷
Release: 2023-03-27 21:34:01
Original
1983 people have browsed it

This article mainly introduces the method of obtaining the number of days difference between a given date in PHP. It analyzes two methods of calculating the number of days difference between dates based on specific examples. It involves related operation skills of PHP date string conversion. Friends in need can refer to it. The details below are as follows:

Method 1:


##

<?php
function count_days($a,$b){
 $a_dt=getdate($a);
 $b_dt=getdate($b);
 $a_new=mktime(12,0,0,$a_dt[&#39;mon&#39;],$a_dt[&#39;mday&#39;],$a_dt[&#39;year&#39;]);
 $b_new=mktime(12,0,0,$b_dt[&#39;mon&#39;],$b_dt[&#39;mday&#39;],$b_dt[&#39;year&#39;]);
 return round(abs($a_new-$b_new)/86400);
}
//今天与2017年8月26日相差多少天
$date1=strtotime(date("Y-m-d"));
$date2=strtotime(&#39;2017-8-26&#39;);
$result=count_days($date1,$date2);
echo $result;
?>
Copy after login

Running results: 187

Method 2:

<?php
//今天与2017年8月26日相差多少天
$Date_1=date("Y-m-d");
$Date_2="2017-8-26";
$d1=strtotime($Date_1);
$d2=strtotime($Date_2);
$Days=round(($d2-$d1)/3600/24);
echo "今天与2017年8月26日相差".$Days."天";
?>
Copy after login

Run result:

今天与2017年8月26日相差187天
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP implementation for dates, months,

days

, weeks, hours, minutes, Methods for addition and subtraction of seconds and so on
PHP method for calculating date intervals

number of days


phpGets the number of days difference between a given date


The above is the detailed content of How to get the number of days difference between a given date in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
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!