How to Increment a Date in PHP
The PHP language provides a number of standard functions for handling dates. Using these functions, you can easily increment a date, adding a single day to it. This may be the current date or any other date you have listed in your PHP script. Incrementing a date is a common practice, particularly within websites where users are planning some event or purchase. Incrementing a date in PHP is not complicated, so the task can be carried out quickly using only a small amount of code.
Instructions
-
-
1
Prepare the PHP script. If you do not already have one, open a new file in a text editor, save it with ".php" extension and enter the following outline code:
<html>
<head></head>
<body>
<?php
//PHP code here
?>
</body>
</html>
The PHP code will be placed between the opening and closing PHP script tags (before the "?>" tag). You can include any HTML you like outside the PHP section of the page.
-
2
Create a date in your PHP code. Using the PHP date function, your code can model a single date, including processing and displaying it for user interaction. This example creates a date variable to model the current date at the time the script is executed:
//models today's date in the format "25 December 2013"
$currentDate = date("d F Y");
echo "<p>Today's date is: ".$currentDate."</p>";
This code excerpt uses the date function to return a text string containing the current date in a format that is recognizable within PHP. The format is also readable to users, so the echo command writes it to the browser for display.
-
-
3
Add to your date. To add one day to your date, incrementing it, use the "strtotime" function, which converts strings into timestamps. The following code adds one day to the current date, saving the result in a variable:
$incrementedTime = strtotime("+1 day", strtotime($currentDate));
Note that the result of this code is to store a timestamp variable, not a formatted date.
-
4
Format the incremented date and output it to the user. Using the date function again, but this time on the incremented timestamp variable, create a formatted date string and write it to the user's browser for display:
$tomorrowDate = date("d F Y", $incrementedTime);
echo "<p>Tomorrow's date is: ".$tomorrowDate."</p>";
This code takes the timestamp resulting from incrementing today's date, then uses the date function to format it as a date string, outputting this for display.
-
5
Save the PHP file and upload it to your server. Browse to the appropriate page in a Web browser to test it. Check that the date is incremented correctly.
-
1
Tips & Warnings
You can alter the formatting for the date to display it however you like. You can also use the function on a date other than the current one, by supplying this as a parameter to the first date function. The function defaults to today if you do not do this.
There are lots of possible date formatting options in PHP, which you can see on the official online PHP manual.
Take care if you are performing more advanced numerical calculations on dates in PHP, as it can get very complicated.
References
Resources
- Photo Credit Ablestock.com/AbleStock.com/Getty Images