Solved

Get date of last 30 days

Posted on 2013-01-17
2
854 Views
Last Modified: 2013-01-17
Hi,

I'm using the following function to output dates for a period of time (days).
However, it does output the dates in the wrong order, from newest to oldest.

How do I reverse the order (ex. 28th, 29th, 30th, 1st, 2nd... etc).

function createDatesArray($days) {

	#CLEAR OUTPUT FOR USE
	$output = array();
	
	#SET CURRENT DATE
	$month = date("m");
	$day = date("d");
	$year = date("Y");
	
	#LOOP THROUGH DAYS
	for($i=1; $i<=$days; $i++){
		$output[] = strtotime(date('Y-m-d',mktime(0,0,0,$month,($day-$i),$year)));
	}
	
	#RETURN DATE ARRAY
	return $output;

}

Open in new window


Output
<?php	
# BUILD ARRAY OF DATES USING FUNCTION
$dates = createDatesArray("30");
	
# OUTPUT A LIST OF THE LAST 30 DAYS IN ORDER
foreach($dates as $date) {

	echo '<th scope="col">'.date("jS\nD",$date).'</th>';
}
?>

Open in new window

0
Comment
Question by:kgp43
[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
2 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 38788481
#LOOP THROUGH DAYS
    for($i=$days; $i>=1; $i--){
        $output[] = strtotime(date('Y-m-d',mktime(0,0,0,$month,($day-$i),$year)));
    }
0
 

Author Comment

by:kgp43
ID: 38788618
Perfect, thanks a lot :)
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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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