?
Solved

Get date of last 30 days

Posted on 2013-01-17
2
Medium Priority
?
935 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 2000 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: 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.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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.
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…
Suggested Courses

771 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