All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Add 7 Days To Date PHP

Last Updated : Mar 11, 2024

Add 7 Days To Date PHP

In this tutorial we will show you the solution of add 7 days to date PHP, generally, there are many ways with help of which we can add days to date using PHP.

Let us understand some of them with the help of the example given below.

Step By Step Guide On Add 7 Days To Date PHP :-

Now, in this article, we are going to discuss two main ways to add 7 days to date using PHP, and these ways are:

  • With the help of strtotime() function
  • With the help of the date_add() function

Here below is an example of code that helps you to understand the strtotime() function.

Whereas in the next tutorial, we will understand about date_add() function. So, let us see the code below to understand how to use strtotime() in our codes.

<!DOCTYPE html>
<html>
<head>
   <title> add 7 days to date PHP </title>
</head>
<body>
<h1>
 TalkersCode
</h1>
<h2>
 Add 7 days to date PHP
</h2>
<h3>
 With help of the strtotime() function
</h3>
<?php
// date in which we want to add 7 days
$date = "Nov 07, 2002";
// conversion of string to time
$date = strtotime($date);
// add 7 days to date which is provided
$date = strtotime("+7 day", $date);
// print the output which is required
echo date('M d, Y', $date);
?>
</body>
</html>
  1. As, here we see that we that in above example we show you an example in which HTML and PHP codes are used.
  2. Let us understand the above codes step by step.
  3. Here, first of all, we create a basic structure of HTML, in which we use <!DOCTYPE html> which defines the type of document. And next one is our HTML tags. These tags are paired tags and all the data regarding HTML is written inside these tags.
  4. After we use our head tag which is again paired tag and contains the title and meta information of the webpage. The data written inside the head is not shown on the webpage.
  5. Now, next is our title tag which defines the title of the webpage. The tag which has its closing tag is known as a paired tag. So, this is again a paired tag.
  6. Now, next is the body which is the main tag of HTML. The data which we have written inside the body is shown on the webpage. Mostly all tags which are helpful to show data or information on the screen are written under the body tag.
  7. Here, as above we see inside our PHP codes, we first store the date inside a variable in which we want to add 7 days using PHP.
  8. After that we just convert this date which is in the string to time and add 7 days to this which is also converted into time and then added to our date.
  9. Now, our job is to print the required date which must be in the format in which we send the date. So, we use the date function and specify the format of the date and also provide the function from which we have to print the date.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to add 7 days to date using PHP.

I hope this tutorial on add 7 days to date PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪