Solved

how to increment dates in php?

Posted on 2012-04-03
6
279 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

831 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