How to Subtract Days From Date in PHP Tutorial

Share this Article
Reading Time: 3 minutes
2,731 Views

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

We need this concept i.e adding days, remove days 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 days from date. This concept will give you the understanding of subtracting 5 days, 7 days, 30 days etc from date.

Learn More –

Let’s get started.


Example 1: Subtract 5 days 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 days'));
  
    echo $newDate;
  
?>
  

Output

2021-10-10

Example 2: Subtract 10 days 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-20";
    $newDate = date('Y-m-d', strtotime($date. ' - 10 days'));
  
    echo $newDate;
  
?>
  

Output

2021-10-10

Example 3: Subtract 5 days 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 days'));
  
    echo $newDate;
  
?>
  

Output

2021-10-29

Example 4: Subtract 10 days 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(' - 10 days'));
  
    echo $newDate;
  
?>
  

Output

2021-10-24

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

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5