0

我正在尝试使用\DateTime::createFromFormat .

但我无法找到两个相同日期之间的区别。就像一天一样,它应该显示一天。下面是我的代码。

    $fromDate = \DateTime::createFromFormat('M j, Y', $request->get('fromDateVal')); 
    $toDate = \DateTime::createFromFormat('M j, Y', $request->get('toDateVal')); 
    $diffdays = $toDate->diff($fromDate) ;

任何帮助都会有所帮助。

我得到这个

    DateInterval {#7658
  interval: 0s
  +"y": 0
  +"m": 0
  +"d": 0
  +"h": 0
  +"i": 0
  +"s": 0
  +"f": 0.0
  +"weekday": 0
  +"weekday_behavior": 0
  +"first_last_day_of": 0
  +"invert": 0
  +"days": 0
  +"special_type": 0
  +"special_amount": 0
  +"have_weekday_relative": 0
  +"have_special_relative": 0
}

4

2 回答 2

1

使用 DateTime::diff() 函数

DateTime::diff() 函数是 PHP 中的一个内置函数,用于返回两个给定 DateTime 对象之间的差异。

<?php

// Initialising the two datetime objects 

$datetime1 = new DateTime('2019-9-10'); 
$datetime2 = new DateTime('2019-9-15'); 

// Calling the diff() function on above 
$difference = $datetime1->diff($datetime2); 

// Getting the difference between two 
echo $difference->format('%R%a days'); 

?>
于 2019-12-10T09:13:40.130 回答
0

做就是了

if($fromDate == $toDate){
    $diffdays = 0;
}

我不知道这是不是你想要的。但是基本上加个条件,看是否一样,加上你想要的值。我加了 0,因为我认为这就是应该的区别。

于 2019-12-10T09:17:32.373 回答