php 将 1 天添加到 DATETIME 格式值

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

adding 1 day to a DATETIME format value

phpmysqldatedatetime

提问by ian

In certain situations I want to add 1 day to the value of my DATETIME formatted variable:

在某些情况下,我想将 1 天添加到我的 DATETIME 格式变量的值中:

$start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"));

What is the best way to do this?

做这个的最好方式是什么?

采纳答案by RaYell

If you want to do this in PHP:

如果您想在 PHP 中执行此操作:

// replace time() with the time stamp you want to add one day to
$startDate = time();
date('Y-m-d H:i:s', strtotime('+1 day', $startDate));

If you want to add the date in MySQL:

如果要在 MySQL 中添加日期:

-- replace CURRENT_DATE with the date you want to add one day to
SELECT DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY);

回答by John Conde

There's more then one way to do this with DateTimewhich was introduced in PHP 5.2. Unlike using strtotime()this will account for daylight savings time and leap year.

在 PHP 5.2 中引入的DateTime有不止一种方法可以做到这一点。与使用strtotime()这将考虑夏令时和闰年不同。

$datetime = new DateTime('2013-01-29');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');

// Available in PHP 5.3

$datetime = new DateTime('2013-01-29');
$datetime->add(new DateInterval('P1D'));
echo $datetime->format('Y-m-d H:i:s');

// Available in PHP 5.4

echo (new DateTime('2013-01-29'))->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');

// Available in PHP 5.5

$start = new DateTimeImmutable('2013-01-29');
$datetime = $start->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');

回答by Dmitry

The DateTimeconstructor takes a parameter string time. $timecan be different things, it has to respect the datetime format.

日期时间的构造函数需要一个参数string time$time可以是不同的东西,它必须尊重日期时间格式

There are some valid values as examples :

有一些有效值作为示例:

  • 'now'(the default value)
  • 2017-10-19
  • 2017-10-19 11:59:59
  • 2017-10-19 +1day
  • 'now'(默认值)
  • 2017-10-19
  • 2017-10-19 11:59:59
  • 2017-10-19 +1day

So, in your case you can use the following.

因此,在您的情况下,您可以使用以下内容。

$dt = new \DateTime('now +1 day'); //Tomorrow
$dt = new \DateTime('2016-01-01 +1 day'); //2016-01-02

回答by nickf

  1. Use strtotimeto convert the string to a time stamp
  2. Add a day to it (eg: by adding 86400 seconds (24 * 60 * 60))
  1. 使用strtotime的字符串转换为时间戳
  2. 添加一天(例如:通过添加 86400 秒(24 * 60 * 60))

eg:

例如:

$time = strtotime($myInput);
$newTime = $time + 86400;

If it's only adding 1 day, then using strtotime again is probably overkill.

如果它只增加 1 天,那么再次使用 strtotime 可能是矫枉过正。

回答by Omar

You can use

您可以使用

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

回答by Pawka

I suggest start using Zend_Dateclasses from Zend Framework. I know, its a bit offtopic, but I'll like this way :-)

我建议开始使用Zend Framework 中的Zend_Date类。我知道,这有点离题,但我会喜欢这种方式:-)

$date = new Zend_Date();
$date->add('24:00:00', Zend_Date::TIMES);
print $date->get();

回答by user3216114

You can use as following.

您可以使用如下。

$start_date = date('Y-m-d H:i:s');
$end_date = date("Y-m-d 23:59:59", strtotime('+3 days', strtotime($start_date)));

You can also set days as constant and use like below.

您还可以将天数设置为常量并按如下方式使用。

if (!defined('ADD_DAYS')) define('ADD_DAYS','+3 days');
$end_date = date("Y-m-d 23:59:59", strtotime(ADD_DAYS, strtotime($start_date)));

回答by Vadim Samokhin

There is a more concise and intuitive way to add days to php date. Don't get me wrong, those php expressions are great, but you always have to google how to treat them. I miss auto-completion facility for that.

有一种更简洁直观的方法可以为 php 日期添加天数。不要误会我的意思,那些 php 表达式很棒,但你总是要谷歌如何对待它们。我想念自动完成功能。

Here is how I like to handle those cases:

以下是我处理这些情况的方式:

(new Future(
    new DateTimeFromISO8601String('2014-11-21T06:04:31.321987+00:00'),
    new OneDay()
))
    ->value();

For me, it's way more intuitive and autocompletion works out of the box. No need to google for the solution each time.

对我来说,它更直观,自动完成功能开箱即用。无需每次都谷歌搜索解决方案。

As a nice bonus, you don't have to worry about formatting the resulting value, it's already is ISO8601 format.

作为一个不错的奖励,您不必担心格式化结果值,它已经是 ISO8601 格式。

This is meringue library, there are more examples here.

这是酥皮库,还有更多的例子在这里

回答by Niroshan

Using server request time to Add days. Working as expected.

使用服务器请求时间来添加天数。按预期工作。

25/08/19 => 27/09/19

25/08/19 => 27/09/19

$timestamp = $_SERVER['REQUEST_TIME'];
$dateNow = date('d/m/y', $timestamp);
$newDate = date('d/m/y', strtotime('+2 day', $timestamp));

Here '+2 days' to add any number of days.

此处“+2 天”可添加任意天数。