search
HomeBackend DevelopmentPHP ProblemPHP function application returns the number of days difference between two given dates

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

Aug 27, 2021 pm 06:43 PM
date_diff()phpstrtotime()functionTime difference

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) ."天";
?>

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) ." 天";
?>

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools