PHP 获取开始日期和结束日期之间的天数

发布于 2024-09-27 09:38:12 字数 113 浏览 2 评论 0原文

如果我有两个变量 $startDate="YYYYmmdd"$endDate="YYYYmmdd",我怎样才能获得它们之间的天数?

谢谢。

If I have two variables $startDate="YYYYmmdd" and $endDate="YYYYmmdd", how can I get the number of days between them please?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

早茶月光 2024-10-04 09:38:12

如果您使用的是 PHP 5.3,则可以使用新的 DateTime 类:

$startDate = new DateTime("20101013");
$endDate = new DateTime("20101225");

$interval = $startDate->diff($endDate);

echo $interval->days . " until Christmas"; // echos 73 days until Christmas

如果没有,您将需要使用 strtotime

$startDate = strtotime("20101013");
$endDate = strtotime("20101225");

$interval = $endDate - $startDate;
$days = floor($interval / (60 * 60 * 24));

echo $days . " until Christmas"; // echos 73 days until Christmas

If you are using PHP 5.3, you can use the new DateTime class:

$startDate = new DateTime("20101013");
$endDate = new DateTime("20101225");

$interval = $startDate->diff($endDate);

echo $interval->days . " until Christmas"; // echos 73 days until Christmas

If not, you will need to use strtotime:

$startDate = strtotime("20101013");
$endDate = strtotime("20101225");

$interval = $endDate - $startDate;
$days = floor($interval / (60 * 60 * 24));

echo $days . " until Christmas"; // echos 73 days until Christmas
土豪我们做朋友吧 2024-10-04 09:38:12
<?php   
 $time1=strtotime($startDate);
    $time2=strtotime($endDate);
    $daycount=floor(($time2-$time1)/ 86400);
?>
<?php   
 $time1=strtotime($startDate);
    $time2=strtotime($endDate);
    $daycount=floor(($time2-$time1)/ 86400);
?>
叶落知秋 2024-10-04 09:38:12
<?php
function days($date1, $date2) {
    $date1 = strtotime($date1);
    $date2 = strtotime($date2);
    return ($date2 - $date1) / (24 * 60 * 60);
}
$date1 = '20100820';
$date2 = '20100930';
echo days($date1, $date2);
?>
<?php
function days($date1, $date2) {
    $date1 = strtotime($date1);
    $date2 = strtotime($date2);
    return ($date2 - $date1) / (24 * 60 * 60);
}
$date1 = '20100820';
$date2 = '20100930';
echo days($date1, $date2);
?>
把回忆走一遍 2024-10-04 09:38:12

这是示例代码

$startDate = mktime(0,0,0,1,1,2010); 
$endDate = mktime(0,0,0,12,1,2010); 

$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Differernce is $fullDays days"; 

Here is the sample code

$startDate = mktime(0,0,0,1,1,2010); 
$endDate = mktime(0,0,0,12,1,2010); 

$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Differernce is $fullDays days"; 
各自安好 2024-10-04 09:38:12

我发现获取它们之间的天数的最简单方法是将开始日期和结束日期转换为 Unix 时间戳并对其进行减法。

然后,如果您想格式化日期,请使用 PHP 日期函数将其转换回来。

The easiest way I have found to get the number of days between them is by converting the Start and End dates to Unix timestamps and doing an subtract on them.

Then if you want to format the date convert it back using the PHP date function.

南巷近海 2024-10-04 09:38:12
$DayDiff = strtotime("2010-01-12")-strtotime("2009-12-30");
echo  date('z', $DayDiff)." Days";

这个应该是精确的并且可以与 PHP << 5.2

$DayDiff = strtotime("2010-01-12")-strtotime("2009-12-30");
echo  date('z', $DayDiff)." Days";

this one should be precise and usable with PHP < 5.2

信仰 2024-10-04 09:38:12

这是我的方法,在大多数情况下基于残酷的搜索,只是因为按秒除法(周、月、年)可能不会返回精确的结果,例如在处理闰年时。

<?php
function datediff( $timeformat, $startdate, $enddate )
{
    $unix_startdate = strtotime( $startdate ) ;
    $unix_enddate = strtotime( $enddate ) ;
    $min_date = min($unix_startdate, $unix_enddate);
    $max_date = max($unix_startdate, $unix_enddate);
    $Sd = date( "d", $unix_startdate ) ;
    $Sm = date( "m", $unix_startdate ) ;
    $Sy = date( "Y", $unix_startdate ) ;
    $Ed = date( "d", $unix_enddate ) ;
    $Em = date( "m", $unix_enddate ) ;
    $Ey = date( "Y", $unix_enddate ) ;

    $unixtimediff = $unix_enddate - $unix_startdate ;
    if ( $unixtimediff <= 0 ) return -1 ;

    switch( strtolower( $timeformat ) )
    {
         case "d": // days
         $divisor = 3600 * 24 ;
         return floor( $unixtimediff / $divisor ) + 1 ; 
         break ;
         case "w": // weeks
         $i = 0 ;
         while ( ( $min_date = strtotime("+1 DAY", $min_date) ) <= $max_date) $i++;
         return floor( $i / 7 ) ;
         break ;
         case "m": // months
         $i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
         while ( ( $min_date = strtotime("+1 MONTH", $min_date) ) <= $max_date) $i++;
         return $i ;
         break ;
         case "q": // quaterly (3 months)
         $i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
         while ( ( $min_date = strtotime("+3 MONTH", $min_date) ) <= $max_date) $i++;
         return $i ;
         break ;
         case "y": // year
         $i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
         while ( ( $min_date = strtotime("+1 MONTH", $min_date) ) <= $max_date) $i++;
         return floor( $i / 12 ) ;
         break ;
    }
}

$startdate = "2014-01-01" ;
$enddate = "2015-12-31" ;
$formats = array( "d" => "days", "w" => "weeks", "m" => "months", "q" => "quaterly", "y" => "years" ) ;
foreach( $formats AS $K => $F )
echo "From $startdate to $enddate in $F format: ". datediff( "$K",  $startdate, $enddate )."<br>" ;

?>

Here's my approach, based upon a brutal search in most cases, just because divisions by seconds (for weeks, months, years) may not return precise results, as while working with leap years for example.

<?php
function datediff( $timeformat, $startdate, $enddate )
{
    $unix_startdate = strtotime( $startdate ) ;
    $unix_enddate = strtotime( $enddate ) ;
    $min_date = min($unix_startdate, $unix_enddate);
    $max_date = max($unix_startdate, $unix_enddate);
    $Sd = date( "d", $unix_startdate ) ;
    $Sm = date( "m", $unix_startdate ) ;
    $Sy = date( "Y", $unix_startdate ) ;
    $Ed = date( "d", $unix_enddate ) ;
    $Em = date( "m", $unix_enddate ) ;
    $Ey = date( "Y", $unix_enddate ) ;

    $unixtimediff = $unix_enddate - $unix_startdate ;
    if ( $unixtimediff <= 0 ) return -1 ;

    switch( strtolower( $timeformat ) )
    {
         case "d": // days
         $divisor = 3600 * 24 ;
         return floor( $unixtimediff / $divisor ) + 1 ; 
         break ;
         case "w": // weeks
         $i = 0 ;
         while ( ( $min_date = strtotime("+1 DAY", $min_date) ) <= $max_date) $i++;
         return floor( $i / 7 ) ;
         break ;
         case "m": // months
         $i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
         while ( ( $min_date = strtotime("+1 MONTH", $min_date) ) <= $max_date) $i++;
         return $i ;
         break ;
         case "q": // quaterly (3 months)
         $i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
         while ( ( $min_date = strtotime("+3 MONTH", $min_date) ) <= $max_date) $i++;
         return $i ;
         break ;
         case "y": // year
         $i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
         while ( ( $min_date = strtotime("+1 MONTH", $min_date) ) <= $max_date) $i++;
         return floor( $i / 12 ) ;
         break ;
    }
}

$startdate = "2014-01-01" ;
$enddate = "2015-12-31" ;
$formats = array( "d" => "days", "w" => "weeks", "m" => "months", "q" => "quaterly", "y" => "years" ) ;
foreach( $formats AS $K => $F )
echo "From $startdate to $enddate in $F format: ". datediff( "$K",  $startdate, $enddate )."<br>" ;

?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文