Php:将可变金额天添加到Y-m-d日期格式
i'm trying to add days to a date with the 'Y-m-d' format:
$oldDate = '2013-05-15';
$newDate = date('Y-m-d', strtotime($oldDate. " + 5 days"));
This ouputs '2013-5-20', but below:
$oldDate = '2013-05-15';
$addedDays = 5;
$newDate = date('Y-m-d', strtotime($oldDate. " + $addedDays days"));
doesn't work, it only outputs '1970-01-01', which doesn't make sense because i only tried to put the days to be added in a variable. They're basically the same code. I appreciate the help trying to understand this. Thanks!
我正在尝试使用'Ymd'格式添加日期: p> \ n
$ oldDate ='2013-05-15';
$ newDate = date('Ym-d',strtotime($ oldDate。“+ 5 days”));
code> pre>
此输出'2013-5-20',但低于: p>
$ oldDate ='2013-05-15'; \ n $ addedDays = 5;
$ newDate = date('Ym-d',strtotime($ oldDate。“+ $ addedDays days”));
code> pre>
不起作用,它只输出'1970-01-01',这没有意义,因为我只试图将日期添加到变量中。 它们基本上是相同的代码。 我很感激帮助我们理解这一点。 谢谢! p>
div>
However the code is right and works, just in case try
$newDate = date('Y-m-d', strtotime($oldDate. " + {$addedDays} days"));
Use the DateTime class. It will spare you a lot of head-ache.
// Create a DateTime object
$date = new DateTime('2013-05-15');
// Original date
echo $date->format('d. F Y'), '<br>';
// Add 5 days
$date->modify('+5 days');
// Modified date
echo $date->format('d. F Y'), '<br>';
I had checked it, but it doesn't work on my computer (likely due to the PHP version). I found an alternative solution, though:
$timeBase = time();
$sDays2change = '+182'; // 6 months
$newtime = strtotime($sDays2change . ' day', $timeBase);
echo date('d/m/Y', $newtime);