w3resource

PHP Date Exercises : Increment date by one month


24. Increment Date by One Month

Write a PHP script to increment date by one month.

Sample date : 2012-12-21

Sample Solution:

PHP Code:

<?php
// Convert the date string "2012-12-21" to a Unix timestamp
$dt = strtotime("2012-12-21");

// Add 1 month to the given date using strtotime() function
// and output the result in the format "Y-m-d"
echo date("Y-m-d", strtotime("+1 month", $dt))."\n";
?>

Output:

2013-01-21

Explanation:

In the exercise above,

  • $dt = strtotime("2012-12-21");: Converts the date string "2012-12-21" to a Unix timestamp using the "strtotime()" function.
  • echo date("Y-m-d", strtotime("+1 month", $dt))."\n";: Adds 1 month to the given date using the "strtotime()" function with the "+1 month" argument, and then formats the result as "Year-Month-Day" using the "date(") function. Finally, it prints the resulting date.
  • Flowchart :

    Flowchart: Increment date by one month

    For more Practice: Solve these Related Problems:

    • Write a PHP script to take a date input and output the date exactly one month later, handling year transitions correctly.
    • Write a PHP function that adds one month to a given date and returns the new date in ISO format.
    • Write a PHP program to increment a date by one month and then compare it with the original date to display the difference in days.
    • Write a PHP script to calculate the next month's date by using DateTime::modify() and then output the result in a user-friendly format.

    Go to:


    PREV : Current Month and Previous Three Months.
    NEXT : Current Date in Italian.

    PHP Code Editor:



    Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.