user1038814
user1038814

Reputation: 9647

Add days to a date in PHP

Is there any php function available where I can add days to a date to make up another date? For example, I have a date in the following format: 27-December-2011

If I add 7 to the above, it should give: 03-January-2012.

Many thanks

Upvotes: 14

Views: 41620

Answers (7)

Wasim Khan
Wasim Khan

Reputation: 1237

$registered = $udata->user_registered;
$registered = date( "d m Y", strtotime( $registered ));
$challanexpiry = explode(' ', $registered);
$day   = $challanexpiry[0];
$month = $challanexpiry[1];
$year  = $challanexpiry[2];
$day = $day+10;
$bankchallanexpiry = $day . " " . $month . " " . $year;

Upvotes: 0

Lucian Minea
Lucian Minea

Reputation: 1336

Actually it's easier than all that.

$some_var = date("Y-m-d",strtotime("+7 day"))

You can use a variable instead of the string, of course. It will be great if the people answering the questions, won't complicate things. Less code, means less time to waste on the server ;).

Upvotes: 2

Smamatti
Smamatti

Reputation: 3931

date('Y-m-d', strtotime('+6 days', strtotime($original_date)));

Upvotes: 5

Aurelio De Rosa
Aurelio De Rosa

Reputation: 22142

You can use the add method of DateTime. Anyway this solution works for php version >= 5.3

Upvotes: 5

Simone
Simone

Reputation: 21262

Look at this simple snippet

$date = date("Y-m-d");// current date

$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");

Upvotes: 9

Matthew
Matthew

Reputation: 48284

$date = new DateTime('27-December-2011');
$date->add(new DateInterval('P7D'));
echo $date->format('d-F-Y') . "\n";

Change the format string to be whatever you want. (See the documentation for date()).

Upvotes: 1

Fabrizio
Fabrizio

Reputation: 3776

Try this

$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days));

Upvotes: 18

Related Questions