How do I add a specified number of days to a date?

Question:
Suppose you have a website where you need to set or display an expiry date 7 days from todays date. How do you do that?
Answer:
The technique is to add 7 days as represented in seconds to todays date as represented in UNIX timestamp and then convert the result into a date format you want.

$today = date('U');
$days7 = 7 * 86400;
 
$end_date = $today + $days7;
$end_date = date('dS of F Y', $end_date);
 
echo "<p>Library book is due back on the $end_date</p>";

Additional Notes
86400 is the number of seconds in a day.

Comments

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.