从DateTime Diff获取剩余的天数

时间:2019-08-21 03:11:19

标签: php datetime

我有以下代码,该代码从两个DateTime对象创建一个差异。

    $fd1=new \DateTime(date("Y-m-d H:i:s"));
    $fd2=new \DateTime();
    $fd2->setTimestamp($agreements[$i]->end_date);
    $diff = $fd2->diff($fd1);

我试图将剩余的天数从今天的$fd1导出到$fd2,这是将来的日期。

这是我的尝试。不过,这是有问题的,因为有些月份有30天,有些月份有31

    $agreements[$i]->remaining_date = $diff->d+($diff->m*31)+($diff->y*365);

我要寻找的是从今天到结束日期的正确剩余天数

2 个答案:

答案 0 :(得分:0)

您可以将DateInterval::format def __enter__(self): # Some SQLite schema alterations need foreign key constraints to be # disabled. Enforce it here for the duration of the schema edition. if not self.connection.disable_constraint_checking(): raise NotSupportedError( 'SQLite schema editor cannot be used while foreign key ' 'constraint checks are enabled. Make sure to disable them ' 'before entering a transaction.atomic() context because ' 'SQLite does not support disabling them in the middle of ' 'a multi-statement transaction.' ) return super().__enter__() 格式字符一起使用,这将为您提供两个a对象之间的总天数:

DateTime

Demo on 3v4l.org

答案 1 :(得分:0)

也许是这样的:

$fd1=new \DateTime(date("Y-m-d H:i:s"));
$fd2=date_create("2020-02-10");
$diff=date_diff($fd1,$fd2);

echo $diff->format("Remaining days: %a.");

尝试输入代码lazy loading