PHP
PHP: Add Date, Months, Years to current date
Did you ever want to add date, months or years to current date in PHP but never figured out how to? Well, this little snippet can come in handy.
PHP Add Date, Months, Years to current date
//For simplicity, I have selected American date format as I am used to it. //You may select any format that your application uses, such as date("d-m-Y") //or date("Y-m-d"), etc. $current_date = date("m-d-Y"); echo "Current Date: ".$current_date."<br />"; //Add 1 Day to current date i.e. today $add_day = strtotime(date("m-d-Y", strtotime($current_date)) . "+1 day"); echo "After adding 1 Day: ".date('m-d-y', $add_day)."<br />"; //Add 10 Days to current date $add_days = strtotime(date("m-d-Y", strtotime($current_date)) . "+10 day"); echo "After adding 10 Days: ".date('m-d-Y', $add_days)."<br />"; //Add 1 Month to current date $add_month = strtotime(date("m-d-Y", strtotime($current_date)) . "+1 month"); echo "After adding 1 Month: ".date('m-d-Y', $add_month)."<br />"; //Add 10 Months to current date $add_months = strtotime(date("m-d-Y", strtotime($current_date)) . "+10 month"); echo "After adding 10 Months: ".date('m-d-Y', $add_months)."<br />"; //Add 1 Year to current date $add_year = strtotime(date("m-d-Y", strtotime($current_date)) . "+1 year"); echo "After adding 1 Year: ".date('m-d-Y', $add_year)."<br />"; //Add 10 Years to current date $add_years = strtotime(date("m-d-Y", strtotime($current_date)) . "+10 year"); echo "After adding 10 Years: ".date('m-d-Y', $add_years)."<br />"; //Results: Current Date: 02-04-2012 After adding 1 Day: 02-05-12 After adding 10 Days: 02-14-2012 After adding 1 Month: 03-04-2012 After adding 10 Months: 12-04-2012 After adding 1 Year: 02-04-2013 After adding 10 Years: 02-04-2022
That’s it!
I am sure that there are other ways to do this. Do you know of any other ways to add date, month and year using PHP? If yes, we would love to hear from you. Please leave your comments below.