PHP,从日期获取明天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14460518/
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
PHP, Get tomorrows date from date
提问by Justin
I have a PHP date in the form of 2013-01-22and I want to get tomorrows date in the same format, so for example 2013-01-23.
我有一个形式为的 PHP 日期,2013-01-22我想以相同的格式获取明天的日期,例如2013-01-23.
How is this possible with PHP?
PHP怎么可能做到这一点?
回答by John Conde
Use DateTime
使用日期时间
$datetime = new DateTime('tomorrow');
echo $datetime->format('Y-m-d H:i:s');
Or:
或者:
$datetime = new DateTime('2013-01-22');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
Or:
或者:
$datetime = new DateTime('2013-01-22');
$datetime->add(new DateInterval("P1D"));
echo $datetime->format('Y-m-d H:i:s');
Or in PHP 5.4+:
或者在 PHP 5.4+ 中:
echo (new DateTime('2013-01-22'))->add(new DateInterval("P1D"))
->format('Y-m-d H:i:s');
回答by Laura Chesches
$tomorrow = date("Y-m-d", strtotime('tomorrow'));
or
或者
$tomorrow = date("Y-m-d", strtotime("+1 day"));
Help Link: STRTOTIME()
帮助链接:STRTOTIME()
回答by Rudi Visser
Since you tagged this with strtotime, you can use it with the +1 daymodifier like so:
由于您使用strtotime标记了它,+1 day因此您可以将其与修饰符一起使用,如下所示:
$tomorrow_timestamp = strtotime('+1 day', strtotime('2013-01-22'));
That said, it's a much better solution to use DateTime.
也就是说,使用 DateTime是一个更好的解决方案。
回答by andy
<? php
//1 Day = 24*60*60 = 86400
echo date("d-m-Y", time()+86400);
?>
回答by ABDUL JAMAL
echo date ('Y-m-d',strtotime('+1 day', strtotime($your_date)));
echo date ('Y-m-d',strtotime('+1 day', strtotime($your_date)));
回答by Gregorio
Use DateTime:
使用DateTime:
To get tomorrow from now :
从现在开始明天:
$d = new DateTime('+1day');
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;
Results : 28/06/2017 08.13.20
结果 : 28/06/2017 08.13.20
To get tomorrow from a date :
要从某个日期获得明天:
$d = new DateTime('2017/06/10 08.16.35 +1day')
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;
Results : 11/06/2017 08.16.35
结果 : 11/06/2017 08.16.35
Hope it helps!
希望能帮助到你!
回答by Diogo Neves - Mangaru
By strange it can seem it works perfectly fine: date_create( '2016-02-01 + 1 day' );
奇怪的是,它似乎工作得很好: date_create( '2016-02-01 + 1 day' );
echo date_create( $your_date . ' + 1 day' )->format( 'Y-m-d' );
echo date_create( $your_date . ' + 1 day' )->format( 'Y-m-d' );
Should do it
应该做
回答by crmpicco
/**
* 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);
}
回答by Vadim Samokhin
First, coming up with correct abstractions is always a key. key to readability, maintainability, and extendability.
首先,提出正确的抽象始终是关键。可读性、可维护性和可扩展性的关键。
Here, quite obvious candidate is an ISO8601DateTime. There are at least two implementations: first one is a parsed datetime from a string, and the second one is tomorrow. Hence, there are two classes that can be used, and their combination results in (almost) desired outcome:
在这里,很明显的候选者是一个ISO8601DateTime. 至少有两种实现:第一种是从字符串中解析出的日期时间,第二种是明天。因此,可以使用两个类,它们的组合会产生(几乎)期望的结果:
new Tomorrow(new FromISO8601('2013-01-22'));
Both objects arean ISO8601 datetime, so their textual representation is not exactly what you need. So the final stroke is to make them take a date-form:
这两个对象都是ISO8601 日期时间,因此它们的文本表示并不完全符合您的需要。所以最后一笔是让他们采取日期形式:
new Date(
new Tomorrow(
new FromISO8601('2013-01-22')
)
);
Since you need a textual representation, not just an object, you invoke a value()method.
由于您需要一个文本表示,而不仅仅是一个对象,因此您需要调用一个value()方法。
For more about this approach, take a look at this post.
有关此方法的更多信息,请查看此帖子。
回答by Muhammad Awais Zulifqar
here's working function
这是工作功能
function plus_one_day($date){
$date2 = formatDate4db($date);
$date1 = str_replace('-', '/', $date2);
$tomorrow = date('Y-m-d',strtotime($date1 . "+1 days"));
return $tomorrow; }

