php 给日期加一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1394791/
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 02:23:33  来源:igfitidea点击:

Adding one day to a date

phpdatedatetime

提问by ian

My code to add one day to a date returns a date before day adding: 2009-09-30 20:24:00date after adding one day SHOULD be rolled over to the next month: 1970-01-01 17:33:29

我将一天添加到日期的代码返回添加前一天的日期: 2009-09-30 20:24:00添加一天后的日期应该滚动到下个月:1970-01-01 17:33:29

<?php

    //add day to date test for month roll over

    $stop_date = date('Y-m-d H:i:s', strtotime("2009-09-30 20:24:00"));

    echo 'date before day adding: '.$stop_date; 

    $stop_date = date('Y-m-d H:i:s', strtotime('+1 day', $stop_date));

    echo ' date after adding one day. SHOULD be rolled over to the next month: '.$stop_date;
?>

I have used pretty similar code before, what am I doing wrong here?

我以前使用过非常相似的代码,我在这里做错了什么?

回答by w35l3y

<?php
$stop_date = '2009-09-30 20:24:00';
echo 'date before day adding: ' . $stop_date; 
$stop_date = date('Y-m-d H:i:s', strtotime($stop_date . ' +1 day'));
echo 'date after adding 1 day: ' . $stop_date;
?>

For PHP 5.2.0+, you may also do as follows:

对于 PHP 5.2.0+,您还可以执行以下操作:

$stop_date = new DateTime('2009-09-30 20:24:00');
echo 'date before day adding: ' . $stop_date->format('Y-m-d H:i:s'); 
$stop_date->modify('+1 day');
echo 'date after adding 1 day: ' . $stop_date->format('Y-m-d H:i:s');

回答by Prasanth Bendra

$date = new DateTime('2000-12-31');

$date->modify('+1 day');
echo $date->format('Y-m-d') . "\n";

回答by minlare

Simplest solution:

最简单的解决方案:

$date = new DateTime('+1 day');
echo $date->format('Y-m-d H:i:s');

回答by Jay Momaya

It Worked for me: For Current Date

它对我有用:对于当前日期

$date = date('Y-m-d', strtotime("+1 day"));

for anydate:

对于任何日期:

date('Y-m-d', strtotime("+1 day", strtotime($date)));

回答by user1987095

Try this

尝试这个

echo date('Y-m-d H:i:s',date(strtotime("+1 day", strtotime("2009-09-30 20:24:00"))));

回答by joan16v

Simple to read and understand way:

简单的阅读和理解方式:

$original_date = "2009-09-29";

$time_original = strtotime($original_date);
$time_add      = $time_original + (3600*24); //add seconds of one day

$new_date      = date("Y-m-d", $time_add);

echo $new_date;

回答by Doug Hays

I always just add 86400 (seconds in a day):

我总是只添加 86400(一天中的秒数):

$stop_date = date('Y-m-d H:i:s', strtotime("2009-09-30 20:24:00") + 86400);

echo 'date after adding 1 day: '.$stop_date; 

It's not the slickest way you could probably do it, but it works!

这不是你能做到的最巧妙的方法,但它有效!

回答by Peter Bailey

While I agree with Doug Hays' answer, I'll chime in here to say that the reason your code doesn't work is because strtotime()expects an INT as the 2nd argument, not a string (even one that represents a date)

虽然我同意 Doug Hays 的回答,但我会在这里插话说你的代码不起作用的原因是因为strtotime()期望一个 INT 作为第二个参数,而不是一个字符串(即使是一个代表日期的)

If you turn on max error reporting you'll see this as a "A non well formed numeric value" error which is E_NOTICE level.

如果您打开最大错误报告,您会看到这是一个 E_NOTICE 级别的“格式不正确的数值”错误。

回答by Kal

The modify()method that can be used to add increments to an existing DateTimevalue.

modify()可用于向现有DateTime值添加增量的方法。

Create a new DateTimeobject with the current date and time:

DateTime使用当前日期和时间创建一个新对象:

$due_dt = new DateTime();

Once you have the DateTimeobject, you can manipulate its value by adding or subtracting time periods:

拥有DateTime对象后,您可以通过添加或减去时间段来操作其值:

$due_dt->modify('+1 day');

You can read more on the PHP Manual.

您可以在PHP 手册上阅读更多内容。

回答by Vadim Samokhin

Since you already have an answer to what's wrong with your code, I can bring another perspective on how you can play with datetimes generally, and solve your problem specifically.

由于您已经对代码的问题有了答案,因此我可以从另一个角度来说明您通常如何处理日期时间,并专门解决您的问题。

Oftentimes you find yourself posing a problem in terms of solution. This is just one of the reasons you end up with an imperativecode. It's great if it works though; there are just other, arguably more maintainable alternatives. One of them is a declarativecode. The point is asking whatyou need, instead of how to get there.

通常,您会发现自己在解决方案方面提出了问题。这只是您最终得到命令式代码的原因之一。如果它有效,那就太好了;还有其他的,可以说是更易于维护的替代方案。其中之一是声明性代码。关键是问你需要什么,而不是如何到达那里。

In your particular case, this can look like the following. First, you need to find out whatis it that you're looking for, that is, discover abstractions. In your case, it looks like you need a date. Not just any date, but the one having some standard representation. Say, ISO8601 date. There are at least two implementations: the first one is a date parsed from an ISO8601-formatted string (or a string in any other format actually), and the second is some future date which is a day later. Thus, the whole code could look like that:

在您的特定情况下,这可能如下所示。首先,你需要找出什么是你正在寻找的,那就是,探索抽象。在您的情况下,您似乎需要一个date。不仅仅是任何日期,而是具有某种标准表示的日期。比如说,ISO8601 日期。至少有两个实现:第一个是从 ISO8601 格式的字符串(或实际上任何其他格式的字符串)解析的日期,第二个是一天后的某个未来日期。因此,整个代码可能如下所示:

(new Future(
    new DateTimeParsedFromISO8601('2009-09-30 20:24:00'),
    new OneDay()
))
    ->value();

For more examples with datetime juggling check out this one.

有关日期时间杂耍的更多示例,请查看这个