Home  >  Article  >  Backend Development  >  How to calculate the difference between dates in php

How to calculate the difference between dates in php

藏色散人
藏色散人Original
2020-08-25 11:01:504692browse

php method to calculate the number of days difference between dates: first create a PHP sample file; then define two times and dates; then calculate through the "round(($d2-$d1)/3600/24);" method The difference between the two dates is only a few days.

How to calculate the difference between dates in php

Recommended: "PHP Video Tutorial"

php calculates the number of days between dates

<?PHP
//今天与2004年10月27日相差多少天
$Date_1=date("Y-m-d");
$Date_2="2004-10-27";
$d1=strtotime($Date_1);
$d2=strtotime($Date_2);
$Days=round(($d1-$d2)/3600/24);
Echo   "今天与2004年10月27日相差".$Days."天";
Echo "<br>";
//今天到2008年9月9日还有多少天
$Date_1=date("Y-m-d");
$Date_2="2008-09-09";
$d1=strtotime($Date_1);
$d2=strtotime($Date_2);
$Days=round(($d2-$d1)/3600/24);
Echo   "今天到2008年9月9日还有".$Days."天";
?>
//---------------------------------------------
                   $arrayStartDate =explode("/",$myStartDate);
                   $arrayEndDate = explode("/",$myEndDate);
                  
                   $jdStartDate = cal_to_jd(CAL_GREGORIAN,$arrayStartDate[1],$arrayStartDate[0],$arrayStartDate[2]);
                   $jdEndDate = cal_to_jd(CAL_GREGORIAN,$arrayEndDate[1],$arrayEndDate[0],$arrayEndDate[2]);
      
                    $duration = $jdEndDate - $jdStartDate + 1;

Statistics of data that will expire in a few days

datediff(yxdate_star,now())

Return number of days = yxdate_star-now()

The above is the detailed content of How to calculate the difference between dates in php. 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