English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Analysis of Methods to Get the Number of Days Between Given Dates in PHP

This article demonstrates how to obtain the number of days between two dates in PHP. Share it for reference, 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['mon'],$a_dt['mday'],$a_dt['year']);
 $b_new=mktime(12,0,0,$b_dt['mon'],$b_dt['mday'],$b_dt['year']);
 return round(abs($a_new-$b_new)/86400);
}
//Today and2017year8month26How many days of difference
$date1=strtotime(date("Y-m-d"));
$date2=strtotime('2017-8-26);
$result=count_days($date1,2);
echo $result;
?>

Running Result:187

Method Two:

<?php
//Today and2017year8month26How many days of difference
$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 ".\$Days=round(\$d2017year8month26Day Difference ".\$Days." days;
?>

Running Result:

Today and2017year8month26Day Difference187day

PS: Here are several time and date-related tools recommended for your reference and use:

Online Date/Date Calculator:
http://tools.jb51.net/jisuanqi/date_jisuanqi

Online Date Calculator/Date Difference Calculator:
http://tools.jb51.net/jisuanqi/datecalc

Online Date Difference Calculator:
http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix Timestamp (timestamp) Conversion Tool:
http://tools.jb51.net/code/unixtime

Readers interested in more PHP-related content can check the special topics on our site: 'PHP Date and Time Usage Summary', 'PHP Array (Array) Operation Skills', 'PHP Basic Syntax Tutorial', 'PHP Operation and Operator Usage Summary', 'PHP Object-Oriented Programming Tutorial', 'PHP Network Programming Skills Summary', 'PHP String (String) Usage Summary', 'PHP'+MySQL Database Operation Tutorial and PHP Common Database Operation Skills Summary

I hope this article will be helpful to everyone in PHP program design.

Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report abuse by email to codebox.com (replace # with @) and provide relevant evidence. Once verified, the site will immediately delete the infringing content.

You May Also Like