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

How to calculate the difference between dates in php

藏色散人
Release: 2023-03-05 08:08:02
Original
4735 people have browsed it

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;
Copy after login

Statistics of data that will expire in a few days

datediff(yxdate_star,now())
Copy after login

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!

Related labels:
php
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