How to Subtract Months From Date in PHP Tutorial

Reading Time: 2 minutes
2,560 Views

Inside this article we will see a basic concept of PHP i.e How to subtract months from date in PHP. Article contains classified information about subtracting months from a specific date or even with current date in PHP.

We need this concept i.e subtracting days, subtracting months from a specific date is when we are creating some record type applications.

In PHP there are several functions which provides the function to subtract number of months from date. This concept will give you the understanding of subtracting 5 months, 7 months etc from date.

Learn More –

Let’s get started.


Example 1: Subtract 5 months from a Specific date

Create a file index.php at your localhost directory.

Open index.php and write this complete code into it.

<?php
  
    $date = "2021-10-15";
    $newDate = date('Y-m-d', strtotime($date. ' - 5 months'));
  
    echo $newDate;
  
?>
  

Output

2021-05-15

Example 2: Subtract 5 months from current date

Create a file index.php at your localhost directory.

Open index.php and write this complete code into it.

<?php
  
    $newDate = date('Y-m-d', strtotime(' - 5 months'));
  
    echo $newDate;
  
?>

Output

2021-06-10

We hope this article helped you to learn How to Subtract Months from Date in PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more