Solved

Get date of last 30 days

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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 …
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 …

746 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

10 Experts available now in Live!

Get 1:1 Help Now