PHP function application returns the number of days difference between two given dates

青灯夜游
Release: 2023-03-12 08:32:02
Original
1913 people have browsed it

In the previous article, we introduced the method of using PHP functions to check whether a given value is finite, infinite or non-numeric. If you are interested, you can click on the link to check → "Checking a given value using PHP functions Whether the value is finite, infinite, or non-numeric》. This time we continue to learn PHP functions and introduce the method of calculating the number of days between two dates. If you need it, you can learn about it~

The focus of this article is to teach you how to calculate the difference between two dates. The number of days between. Here are two methods to introduce to you. You can do it locally first to see if the implementation method is the same as mine.

Let’s first look at the first method: using the strtotime() function

Implementation method:

  • Combine the two dates Use the strtotime() function to convert to a timestamp;

  • Then subtract the two timestamps to get the time difference, but this time it is still counted in seconds, which is not conducive to reading.

  • Then divide the time by 86400 (24*60*60=86400), and then use floor() to round down to the nearest integer

The implementation code is given directly below:

<?php
header("content-type:text/html;charset=utf-8");
function daysDiff($date1, $date2)
{
    $first = strtotime($date1);
    $second = strtotime($date2);
    if($first>$second){
    	$diff_seconds=$first-$second;
    }else{
    	$diff_seconds=$second-$first;
    }
	$time = floor(($diff_seconds)/86400);
    return $time;
}

$dt1 = &#39;2021-8-1&#39;;
$dt2 = &#39;2021-8-27&#39;;
echo $dt1.&#39; 和 &#39;. $dt2. &#39; 之间相差的天数是 &#39;. daysDiff($dt1, $dt2) ."天";
?>
Copy after login

Output result:

PHP function application returns the number of days difference between two given dates

If you find this method a bit troublesome, we will introduce it below Another easier method: Use the date_diff() function

The date_diff() function can directly return the difference between two DateTime objects. We only need to convert the two specified dates into DateTime objects, and then use date_diff(datetime1,datetime2) to get the number of days between them.

The implementation code is given directly below:

<?php
header("content-type:text/html;charset=utf-8");
function daysDiff($date1, $date2)
{
    $date1=date_create($date1);
	$date2=date_create($date2);
	$diff_seconds=date_diff($date1,$date2);


    return $diff_seconds->format("%R%a");
}

$dt1 = &#39;2020-03-15&#39;;
$dt2 = &#39;2021-12-12&#39;;
echo $dt1.&#39; 和 &#39;. $dt2. &#39; 之间相差的天数是 &#39;. daysDiff($dt1, $dt2) ." 天";
?>
Copy after login

Output result:

PHP function application returns the number of days difference between two given dates

That’s it for now, if there is anything else you want to know Yes, you can click this. → →php video tutorial

The above is the detailed content of PHP function application returns the number of days difference between two given dates. For more information, please follow other related articles on the PHP Chinese website!

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!