将当前日期提高5天 [英] Increase current date by 5 days
问题描述
$ date = date('Y-m-d',current_time('timestamp',0));
如何将 $ date
code> $ date + 5天?
PHP版本为5.2。
此代码无效:
$ date_cur = date('Ym-d',current_time('timestamp' ,0));
echo $ date_cur。 '< br>';
$ date_cur_plus = date($ date_cur,strtotime('+ 5 days'));
echo $ date_cur_plus;
给我:
2011-11-29
2011-11-29
您可以使用 mktime()
使用时间戳。
$ date = date('Ym-d',mktime(0,0,0,date('m' date('d')+ 5,date('Y')));
使用 strtotime()
更快,但如果您需要进行大量的修改,我的方法仍然可以工作,并且是灵活的。另外, strtotime()
不能处理模糊的日期。
修改 p>
如果您必须以格式 YYYY-MM-DD
的现有日期字符串添加5天,那么您可以将其拆分为数组,并使用 mktime()
中的这些部分。
$ parts = explode(' - ',$ date);
$ datePlusFive = date(
'Ym-d',
mktime(0,0,0,$ parts [1],$ parts [2] + 5,$ parts [0] )
// ^月^日+ 5 ^年
);
$date = date('Y-m-d',current_time('timestamp', 0));
How do I change $date
to $date + 5 days
?
PHP version is 5.2.
This code doesn't work:
$date_cur = date('Y-m-d',current_time('timestamp', 0));
echo $date_cur . ' <br>';
$date_cur_plus = date($date_cur, strtotime('+5 days'));
echo $date_cur_plus;
Gives me:
2011-11-29
2011-11-29
You could use mktime()
using the timestamp.
Something like:
$date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + 5, date('Y')));
Using strtotime()
is faster, but my method still works and is flexible in the event that you need to make lots of modifications. Plus, strtotime()
can't handle ambiguous dates.
Edit
If you have to add 5 days to an already existing date string in the format YYYY-MM-DD
, then you could split it into an array and use those parts with mktime()
.
$parts = explode('-', $date);
$datePlusFive = date(
'Y-m-d',
mktime(0, 0, 0, $parts[1], $parts[2] + 5, $parts[0])
// ^ Month ^ Day + 5 ^ Year
);
这篇关于将当前日期提高5天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!