开发者

Better way of creating a PHP future date

开发者 https://www.devze.com 2022-12-27 05:58 出处:网络
Is there a quicker way of creating a date such as: echo date(\'Y-m-d\', mktime(0, 0, 0, date(\"m\"), date(\"d\")+3,date(\"Y\")));

Is there a quicker way of creating a date such as:

echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")+3,   date("Y")));
开发者_运维技巧

Thanks if you can help.


How about strtotime():

date('Y-m-d', strtotime('+3 days'));


You will have to look into strtotime(). I'd imagine your final code would look something like this:

$currentDate      = strtotime('today');//your date variable goes here
$futureDate = date('Y-m-d', strtotime('+ 2 days', $currentDate));
echo $futureDate;

Live Demo

If you are using PHP version >= 5.2 I strongly suggest you use the new DateTime object. For example like below:

$futureDate = new DateTime("today");
$futureDate->modify("+2 days");
echo $futureDate->format("Y-m-d");

Live Demo

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号