Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get date of last 30 days

Posted on 2013-01-17
2
Medium Priority
?
995 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
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.
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 …
Suggested Courses

610 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