To add years to a date in PHP, you can use the DateTime
class and its add()
method. Here’s a step-by-step guide on how to do it:
Step 1: Create a DateTime
object representing the initial date.
$initialDate = new DateTime('2023-08-03');
Step 2: Define the number of years you want to add.
$yearsToAdd = 1; // Change this value to the number of years you want to add.
Step 3: Use the add()
method to add the years to the date.
$modifiedDate = $initialDate->add(new DateInterval('P' . $yearsToAdd . 'Y'));
Step 4: Retrieve the modified date as a string in the desired format.
$modifiedDateString = $modifiedDate->format('Y-m-d');
Now, $modifiedDateString
will contain the date with the added year. You can change the format in the format()
function as per your requirements.
Here’s the complete code:
$initialDate = new DateTime('2023-08-03');
$yearsToAdd = 1; // Change this value to the number of years you want to add.
$modifiedDate = $initialDate->add(new DateInterval('P' . $yearsToAdd . 'Y'));
$modifiedDateString = $modifiedDate->format('Y-m-d');
echo $modifiedDateString;
Remember to modify the $yearsToAdd
variable to the desired number of years you want to add.