美文网首页
php 计算两个日期的间隔天数

php 计算两个日期的间隔天数

作者: 哎哟我去 | 来源:发表于2019-10-24 16:18 被阅读0次
<?php 
$start = "2016-05-25";
$end = "2016-05-23";
 
$datetime_start = new DateTime($start);
$datetime_end = new DateTime($end);
var_dump($datetime_start->diff($datetime_end));

结果如下:

object(DateInterval)[3]
  public 'y' => int 0
  public 'm' => int 0
  public 'd' => int 2
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'weekday' => int 0
  public 'weekday_behavior' => int 0
  public 'first_last_day_of' => int 0
  public 'invert' => int 1
  public 'days' => int 2
  public 'special_type' => int 0
  public 'special_amount' => int 0
  public 'have_weekday_relative' => int 0
  public 'have_special_relative' => int 0

由结果我们知道,想要得出时间差,可以用下面方法实现:

$start = "2016-05-25";
$end = "2016-05-23";
 
$datetime_start = new DateTime($start);
$datetime_end = new DateTime($end);
$days = $datetime_start->diff($datetime_end)->days;
echo "时间差是:$days";  //结果是2
  • date_create()、date_diff()实现
$start = "2016-05-25";
$end = "2016-05-23";
 
$datetime_start = date_create($start);
$datetime_end = date_create($end);
$days = date_diff($datetime_start, $datetime_end);
var_dump($days->days);

相关文章

网友评论

      本文标题:php 计算两个日期的间隔天数

      本文链接:https://www.haomeiwen.com/subject/fcldvctx.html