简体   繁体   English

将 1 天添加到 DATETIME 格式值

[英]adding 1 day to a DATETIME format value

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?做这个的最好方式是什么?

There's more then one way to do this with DateTime which was introduced in PHP 5.2.在 PHP 5.2 中引入的DateTime有不止一种方法可以做到这一点。 Unlike using strtotime() this will account for daylight savings time and leap year.与使用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');

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);

The DateTime constructor takes a parameter string time . DateTime构造函数采用参数string time $time can be different things, it has to respect the datetime format . $time可以是不同的东西,它必须尊重datetime 格式

There are some valid values as examples :有一些有效值作为示例:

  • 'now' (the default value) '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
  1. Use strtotime to convert the string to a time stamp使用strtotime将字符串转换为时间戳
  2. Add a day to it (eg: by adding 86400 seconds (24 * 60 * 60))添加一天(例如:通过添加 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 可能是矫枉过正。

You can use您可以使用

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

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)));

I suggest start using Zend_Date classes from Zend Framework .我建议开始使用Zend Framework中的Zend_Date类。 I know, its a bit offtopic, but I'll like this way :-)我知道,这有点离题,但我会喜欢这种方式:-)

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

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 天”可添加任意天数。

There is a more concise and intuitive way to add days to php date.有一种更简洁直观的方法可以为 php 日期添加天数。 Don't get me wrong, those php expressions are great, but you always have to google how to treat them.不要误会我的意思,那些 php 表达式很棒,但你总是要谷歌如何对待它们。 I miss auto-completion facility for that.我想念自动完成功能。

Here is how I like to handle those cases:以下是我喜欢处理这些情况的方式:

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

One liner !一个班轮!

echo (new \DateTime('2016-01-01 +1 day'))->format('Y-m-d H:i:s');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM