faqts : Computers : Programming : Languages : PHP : Sample Code

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 14 people (50%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

how to sort an array containing date values it should sort considering date,monthand year?
how to sort an array containing date values it should sort considering date,monthand year?

Apr 25th, 2007 04:33
Mihir Darji, deepu prasad, PHP Manual Array and Date Functions


Use Following function:


function sortDateArray($arrDate)
	{
		// Intialize internal arrays
		$arrReturnDate=array();
		$arrReturnDate_array();
		// loop thro to make timestamps
		for($i=0;$i<count($arrDate);$i++)
		{
			$arrReturnDate[$i] = strtotime($arrDate[$i]);
			 
		}
		// sort timestamps NUMERIC way
		sort($arrReturnDate,SORT_NUMERIC );
		// rebuild date array
		for($i=0;$i<count($arrReturnDate);$i++)
		{
			$arrReturnDate_[$i] = date('Y-m-d',$arrReturnDate[$i]);
			 
		}
		
		return $arrReturnDate_;
	}
	// let's try it
$arrDate=array('2007-12-07','2006-12-07','2007-07-12','2006-07-12');
	echo "<pre>".print_r(sortDateArray($arrDate),true) . 
print_r($arrDate,true)."</pre>";