Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get current date and date after two months in php

Tags:

date

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 month-date-year (numerical.)

like image 312
Mohit Jain Avatar asked Sep 04 '10 14:09

Mohit Jain


People also ask

How to Add 1 month in current date in PHP?

this tutorial will give you example of add month to current date using php. $date = date("d/m/Y", strtotime(" +1 months"));

How can I get first date and date of last month in PHP?

You can be creative with this. For example, to get the first and last second of a month: $timestamp = strtotime('February 2012'); $first_second = date('m-01-Y 00:00:00', $timestamp); $last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!

How to Add current date in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

How can I get next 10 days in PHP?

echo date ( 'Y-m-d' , strtotime ( $Date . ' + 10 days' ));


2 Answers

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...

like image 61
Yanick Rochon Avatar answered Oct 19 '22 23:10

Yanick Rochon


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);
like image 35
Vishnu Choudhary Avatar answered Oct 19 '22 23:10

Vishnu Choudhary