Solved

Get date of last 30 days

Posted on 2013-01-17
2
812 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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.

815 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

12 Experts available now in Live!

Get 1:1 Help Now