how to increment dates in php?

Posted on 2012-04-03
Last Modified: 2012-04-03
Can someone let me know how I can increment dates in PHP. I know the start date; for eg 2012-04-03. I need the output for the next  20 days in the format,

 Apr 4, Apr 5....., Apr 24.

Thank you for the help.

Question by:aej1973
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Expert Comment

ID: 37803037

Author Comment

ID: 37803055
Thanks, but how do I set up an array to get the output I am looking for. That is where I have a problem.

LVL 10

Assisted Solution

MadShiva earned 100 total points
ID: 37803090

You should use timestamp.

Like this :

$timestamp= strtotime("2012-04-03");

for ($i = 1; $i <= 10; $i++) {
    $timestamp = strtotime("+".$i." day",$timestamp);
    echo date('M d', $timestamp);

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 83

Expert Comment

by:Dave Baldwin
ID: 37803104
Try this.  You can use 'foreach' to get the dates out of the array.

$startdate = "2012-04-03";
$nudates = array();

for($i;$i < 20;$i++) {
  $date = strtotime("+$i day", strtotime($startdate));
  $nudates[] = date("Y-m-d", $date);

Open in new window

LVL 83

Accepted Solution

Dave Baldwin earned 100 total points
ID: 37803124
I like part of MadShiva's version better because it only does the 'strtotime' operation once.

$timestamp= strtotime("2012-04-03");
$nudates = array();

for($i;$i < 20;$i++) {
  $date = strtotime("+$i day", $timestamp);
  $nudates[] = date("M d", $date);

Open in new window


Author Closing Comment

ID: 37803737
Thank you.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question