Solved

how to increment dates in php?

Posted on 2012-04-03
6
281 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there an elegant way to view sql statements written in PDO? 2 25
Read a remote csv file from a https URL 8 49
mysql query for sum() 3 28
PHP 5.6 and 7.x 4 23
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

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