Solved

how to increment dates in php?

Posted on 2012-04-03
6
283 Views
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.

A
0
Comment
Question by:aej1973
[X]
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
6 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 37803037
0
 

Author Comment

by:aej1973
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.

A
0
 
LVL 10

Assisted Solution

by:MadShiva
MadShiva earned 100 total points
ID: 37803090
Dear,

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


Regards
0
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

0
 
LVL 83

Accepted Solution

by:
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

0
 

Author Closing Comment

by:aej1973
ID: 37803737
Thank you.
0

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 (http://en.wikipedia.org/wiki/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 (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  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