PHP - 添加 1 天到日期格式 mm-dd-yyyy
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16018629/
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 - add 1 day to date format mm-dd-yyyy
提问by Jayr
<?php
$date = "04-15-2013";
$date = strtotime($date);
$date = strtotime("+1 day", $date);
echo date('m-d-Y', $date);
?>
This is driving me crazy and seems so simple. I'm pretty new to PHP, but I can't figure this out. The echo returns 01-01-1970
.
这让我发疯,看起来很简单。我对 PHP 很陌生,但我无法弄清楚。回声返回01-01-1970
。
The $date will be coming from a POST
in the format m-d-Y
, I need to add one day and have it as a new variable to be used later.
$date 将来自 aPOST
格式m-d-Y
,我需要添加一天并将其作为新变量以供以后使用。
Do I have to convert $date to Y-m-d
, add 1 day, then convert back to m-d-Y
?
Would I be better off learning how to use DateTime
?
我是否必须将 $date 转换为Y-m-d
,添加 1 天,然后转换回m-d-Y
?我学习如何使用会更好DateTime
吗?
回答by Fabio
there you go
你去吧
$date = "04-15-2013";
$date1 = str_replace('-', '/', $date);
$tomorrow = date('m-d-Y',strtotime($date1 . "+1 days"));
echo $tomorrow;
this will output
这将输出
04-16-2013
Documentation for both function
date
strtotime
两个函数
date strtotime 的文档
回答by John Conde
$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$date->modify('+1 day');
echo $date->format('m-d-Y');
Or in PHP 5.4+
或者在 PHP 5.4+
echo (DateTime::createFromFormat('m-d-Y', '04-15-2013'))->modify('+1 day')->format('m-d-Y');
reference
参考
回答by insoftservice
$date = strtotime("+1 day");
echo date('m-d-y',$date);
回答by mohammad mohsenipur
use http://www.php.net/manual/en/datetime.add.phplike
使用http://www.php.net/manual/en/datetime.add.php 之类的
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 days'));
echo date_format($date, 'Y-m-d');
output
输出
2000-01-2
回答by Randika Vishman
Actually I wanted same alike thing, To get one year backward date, for a given date! :-)
其实我想要同样的东西,为了给定的日期获得一年后的日期!:-)
With the hint of above answer from @mohammad mohsenipur I got to the following link, via his given link!
有了上面的回答从@mohammad mohsenipur提示我到了下面的链接,通过他给出的链接!
Luckily, there is a method same as date_add method, named date_sub method! :-) I do the following to get done what I wanted!
幸运的是,有一个与 date_add 方法相同的方法,名为 date_sub 方法!:-) 我做了以下事情来完成我想要的!
$date = date_create('2000-01-01');
date_sub($date, date_interval_create_from_date_string('1 years'));
echo date_format($date, 'Y-m-d');
Hopes this answer will help somebody too! :-)
希望这个答案也能帮助别人!:-)
Good luck guys!
祝大家好运!
回答by hek2mgl
The format you've used is not recognized by strtotime(). Replace
strtotime() 无法识别您使用的格式。代替
$date = "04-15-2013";
by
经过
$date = "04/15/2013";
Or if you want to use -
then use the following line with the year in front:
或者,如果您想使用,请-
使用以下行和前面的年份:
$date = "2013-04-15";