99

我有一个形式的 PHP 日期,2013-01-22我想以相同的格式获取明天的日期,例如2013-01-23.

PHP怎么可能做到这一点?

4

11 回答 11

225

使用日期时间

$datetime = new DateTime('tomorrow');
echo $datetime->format('Y-m-d H:i:s');

或者:

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

或者:

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

或者在 PHP 5.4+ 中:

echo (new DateTime('2013-01-22'))->add(new DateInterval("P1D"))
                                 ->format('Y-m-d H:i:s');
于 2013-01-22T14:12:23.373 回答
91
 $tomorrow = date("Y-m-d", strtotime('tomorrow'));

或者

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

帮助链接:STRTOTIME()

于 2015-12-15T09:47:56.037 回答
17

由于您使用标记了它,因此您可以将其与+1 day修饰符一起使用,如下所示:

$tomorrow_timestamp = strtotime('+1 day', strtotime('2013-01-22'));

也就是说,使用 DateTime是一个更好的解决方案。

于 2013-01-22T14:14:49.297 回答
15
<? php 

//1 Day = 24*60*60 = 86400

echo date("d-m-Y", time()+86400); 

?>
于 2013-10-24T10:35:41.147 回答
6

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

于 2017-06-22T05:12:23.057 回答
5

使用DateTime

从现在开始明天:

$d = new DateTime('+1day');
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;

结果 : 28/06/2017 08.13.20

要从某个日期获得明天:

$d = new DateTime('2017/06/10 08.16.35 +1day')
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;

结果 : 11/06/2017 08.16.35

希望能帮助到你!

于 2017-06-27T01:18:18.237 回答
1
/**
 * get tomorrow's date in the format requested, default to Y-m-d for MySQL (e.g. 2013-01-04)
 *
 * @param string
 *
 * @return string
 */
public static function getTomorrowsDate($format = 'Y-m-d')
{
    $date = new DateTime();
    $date->add(DateInterval::createFromDateString('tomorrow'));

    return $date->format($format);
}
于 2015-06-09T16:26:07.130 回答
1

奇怪的是,它看起来工作得很好:date_create( '2016-02-01 + 1 day' );

echo date_create( $your_date . ' + 1 day' )->format( 'Y-m-d' );

应该这样做

于 2016-02-02T12:51:20.693 回答
0

首先,提出正确的抽象总是一个关键。可读性、可维护性和可扩展性的关键。

在这里,很明显的候选人是一个ISO8601DateTime。至少有两种实现:第一个是从字符串解析的日期时间,第二个是明天。因此,有两个类可以使用,它们的组合会产生(几乎)期望的结果:

new Tomorrow(new FromISO8601('2013-01-22'));

这两个对象都是ISO8601 日期时间,因此它们的文本表示并不完全符合您的需要。所以最后一招是让它们采用日期形式:

new Date(
    new Tomorrow(
        new FromISO8601('2013-01-22')
    )
);

由于您需要文本表示,而不仅仅是一个对象,因此您调用了一个value()方法。

有关此方法的更多信息,请查看这篇文章

于 2020-05-10T14:24:16.783 回答
-1

这是工作功能

function plus_one_day($date){
 $date2 = formatDate4db($date);
 $date1 = str_replace('-', '/', $date2);
 $tomorrow = date('Y-m-d',strtotime($date1 . "+1 days"));
 return $tomorrow; }
于 2017-01-31T08:53:59.007 回答
-4
$date = '2013-01-22';
$time = strtotime($date) + 86400;
echo date('Y-m-d', $time);

其中 86400 是一天中的秒数。

于 2013-01-22T14:14:27.123 回答