开发者

get current date and date after two months in php

this is a very lame question but i m not able to find this one. How to get today's date and date after two months..

format is mon开发者_开发问答th-date-year (numerical.)


You can use the strtotime() function :

$today = time();
$twoMonthsLater = strtotime("+2 months", $today);

// If what you really want is exactly 60 days later, then
$sixtyDaysLater = strtotime("+60 days", $today);
// ...or 8 weeks later :
$eightWeeksLater = strtotime("+8 weeks", $today);

In any case, the resulting new timestamps can then be converted to month-date-year :

echo 'Today is : ' . date('m-d-Y', $today);
echo 'Two months later will be : ' . date('m-d-Y', $twoMonthsLater);

** UPDATE **

From the PHP manual

Note: Please keep in mind that these functions are dependent on the locale settings of your server. Make sure to take daylight saving time (use e.g. $date = strtotime('+7 days', $date) and not $date += 7*24*60*60) and leap years into consideration when working with these functions.

Just thought I should mention it...


There are a couple of ways you could go about it - the first one would be to do something like this:

echo $currentdate = date("Y-m-d H:i:s",time());    
echo $after60days = date('Y-m-d H:i:s', time() + 60 * 60 * 24 * 60);

Basically, you take the current timestamp, expressed in seconds, and add 60 * 60 * 24 * 60, which is the amount of seconds in two months.

Another way to do it, which is my preferred way and how I would do it, is this:

$after60days = strtotime("+60 days");

The outcome will be exactly the same, $after60days will have a value equal to the timestamp of the day exactly two month from now, but it uses PHP's own strtotime() function.

Of course, if you need to output a date in a format that easy to read for humans, you can do something like this:

echo date('Y-m-d H:i:s',$after60days);


Today:

date('m-d-Y', time());

Two months from now:

date('m-d-Y', time() + (86400 * 60));

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜