How to add months to a given date using PHP

No Comments

By using the PHP functions date and strtotime this can be accomplished.

Example 

$date = '2010-03-22'; 
 
$new_date = date('Y-m-d',strtotime('+4 months',strtotime($date))); 
 
echo $new_date; //Displays 2010-07-22

Explanation of PHP functions:

1)

FUNCTION
date

DESCRIPTION
string
 date ( string $format [, int $timestamp ] )

PARAMETERS USED IN THIS EXAMPLE

Y = A full numeric representation of a year, 4 digits (Examples: 1999 or 2003)
= Numeric representation of a month, with leading zeros (01 through 12)
= Day of the month, 2 digits with leading zeros (01 to 31)

2)

FUNCTION
strtotime

DESCRIPTION
int strtotime ( string $time [, int $now ] )

PARAMETERS

time The string to parse. Before PHP 5.0.0, microseconds weren’t allowed in the time, since PHP 5.0.0 they are allowed but ignored.
now The timestamp which is used as a base for the calculation of relative dates.

Image Credits: Photo by Isis França on Unsplash.

Related Articles

No results found

Leave a Reply

avatar
  Subscribe  
Notify of
Menu
Copy link