Solved

how to increment dates in php?

Posted on 2012-04-03
6
276 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
Comment Utility
0
 

Author Comment

by:aej1973
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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 82

Accepted Solution

by:
Dave Baldwin earned 100 total points
Comment Utility
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
Comment Utility
Thank you.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now