php 日期时间加 1 天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17965135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:44:24  来源:igfitidea点击:

DateTime add 1 day

phpdatetimefor-loop

提问by CreamYGEEK

My Form has 2 fields - Time_from and Time_to

我的表单有 2 个字段 - Time_from 和 Time_to

Now i need to add an entry in my database for each day (If theres is a difference between those days) ex. Time_from = 2013-08-26 08:00:00 and Time_to = 2013-08-28 16:00:00 So in my database i should get 3 entries

现在我需要每天在我的数据库中添加一个条目(如果这些天之间存在差异)例如。Time_from = 2013-08-26 08:00:00 和 Time_to = 2013-08-28 16:00:00 所以在我的数据库中我应该得到 3 个条目

Time_from = 2013-08-26 08:00:00 and Time_to = 2013-08-26 16:00:00
Time_from = 2013-08-27 08:00:00 and Time_to = 2013-08-27 16:00:00
Time_from = 2013-08-28 08:00:00 and Time_to = 2013-08-28 16:00:00

So for that purpose i have made a method, where i first find the difference between those 2 dates and then i'm using a for loop that will run as many days in difference those 2 dates have, and at the end im just adding 1 day.

所以为了这个目的,我做了一个方法,我首先找到这两个日期之间的差异,然后我使用一个 for 循环,该循环将运行这两个日期的差异天数,最后我只添加 1日。

public function createOffer($offer)
{
$time_from = new DateTime($offer->time_from);
$time_to = new DateTime($offer->time_to);
$interval = $time_from->diff($time_to);

for ($counter = 0; $counter <= $interval->d; $counter++) {
    $offer->time_from = $time_from->format('Y-m-d H:i:s');
    $offer_time_to = new DateTime($time_from->format('Y-m-d'));
    $offer_time_to->setTime($time_to->format('H'), $time_to->format('i')
        , $time_to->format('s'));
    $offer->time_to = $offer_time_to->format('Y-m-d H:i:s');

    if ($offer->validate()) {
    $offer->save();
    }

    date_modify($time_from, '+1 day');
    }
}

For some reason, the code doesn't work.

出于某种原因,代码不起作用。

When i remove the date_modify field only the first day will be saved in my database (Guess the for loop is only running once then)

当我删除 date_modify 字段时,只有第一天会保存在我的数据库中(猜猜 for 循环只运行一次)

But with the date_modify in my code, only the last day is saved in the database.

但是在我的代码中使用 date_modify 时,只有最后一天保存在数据库中。

回答by liyakat

you can use Add function of Datatime object

您可以使用 Datatime 对象的 Add 函数

here i am giving you one example to add one 1 in your post date like

在这里,我给你一个例子,在你的发布日期中添加一个 1,比如

<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P1D'));
echo $date->format('Y-m-d') . "\n";
?>

OUTPUT

输出

2000-01-02

Hope it will sure help you to solve your issue.

希望它一定能帮助您解决您的问题。

回答by Marcio Mazzucato

$date = new DateTime('2017-01-17');
$date->modify('+1 day');

echo $date->format('Y-m-d'); // 2017-01-18

You can do it too:

你也可以这样做:

$dateTo = new \DateTime('1980-12-07 +1 day');
echo $dateTo->format('Y-m-d'); // 1980-12-08