Inside this article we will see a basic concept of PHP i.e How to get previous month from date in PHP. Article contains classified information about getting previous months from a specific date or even with current date in PHP.
We need this concept i.e get previous 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 get previous months from date.
Learn More –
- How to Get Next Month From Date in PHP Tutorial
- How To Install Composer in Ubuntu 18.04 ?
- How To Set Sublime Text Editor to FileZilla as Editor ?
- How to Subtract Days From Date in PHP Tutorial
Let’s get started.
Example 1: Get Previous Month 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-03-01"; $newDate = date('Y-m-d', strtotime($date. ' -1 month ')); echo $newDate; ?>
Output
2021-02-01
Example 2: Get Previous Month from Today
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(' -1 month ')); echo $newDate; ?>
Output
2021-10-19
We hope this article helped you to learn How to Get Previous Month from Date in PHP Tutorial in a very detailed way.
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.