Sort Date YYYY MM DD help!

Hi,

I have been trying to use ksort() and usort()  to arrange a list of dates in order from current to last. Currently I have parsed a list of dates from a database into YYYY MM DD format and added them all to an array to be sorted with  ksort, but when the dates print they are not sorted. I know it has something to do with how I'm creating the date array but I'm lost.

Thanks,
waffe
$ISOSDates = array();
	//Select the date in the media(api)data and split it into an array
	//Format the date into ISO (YYYY-MM-DD) and use ksort to put it in order
	for($i = 0; $i < sizeof($videosMostPopularMediaData); $i++){
		$fullDate = $videosMostPopularMediaData[$i]['uploaded_date'];
		//splits the $fulldate array into its components
		list($dayName, $month, $dayNum, $time, $timetype, $year) = split('[ ]', $fullDate);
		//populate the the array with dates to be sorted
		$ISOSDates[$i] = "$year$month$dayNum";
	}
	ksort($ISOSDates);
	print_r($ISOSDates);

Open in new window

waffeAsked:
Who is Participating?
 
waffeAuthor Commented:
Finnal got - little dirty, but its the best I can do at the moment - thanks for the help
	//NEW SHOWS------------------------------------------------------------------------------------------------------------------------------------
	$ISOSDates = array();
	$monthnames = array(01 =>"Jan",02 =>"Feb",03 =>"Mar",04 =>"Apr",05 =>"May",06 =>"Jun",07 =>"Jul",08 =>"Aug",09 =>"Sep",10 =>"Oct",11 =>"Nov",12 =>"Dec"); 
	
	$sortedDateData = array();//holds the dates which get sorted
	$sortedMediaIds = array();//holds the dates 
	
	//The dates need to be ordered current to last------------------------------
	//Select the date in the media(api)data and split it into an array
	//Format the date into ISO (YYYY-MM-DD) and use ksort to put it in order
	for($i = 0; $i < sizeof($mediaData); $i++){
		$fullDate = $mediaData[$i]['uploaded_date'];
		//splits the $fulldate array into its components
		list($dayName, $month, $dayNum, $time, $timetype, $year) = split('[ ]', $fullDate);
		for($j = 0; $j < 13; $j++){//13 because the array index is 0 but the the data Jan starts at 1
			//month name found----------------
			if($month === $monthnames[$j]){
				if($j < 10){
					$monthNum = "0" . $j;
				}else{
					$monthNum = $j;
				}
			}
		}
		//populate the array with the newly formated date
		$ISOSDates[$i] = "$year$monthNum$dayNum";
	}
	//sort the dates
	$sortedDates = sort($ISOSDates, SORT_NUMERIC); 

Open in new window

0
 
DrDamnitCommented:
Why not convert them all t Unix time stamps, then sort, and then use the date function to reformat?
0
 
waffeAuthor Commented:
ok,

any idea why this is not working:
            list($dayName, $month, $dayNum, $time, $timetype, $year) = split('[ ]', $fullDate);
            //echo ($month . $dayNum . $year . " ");
            $unixTimeStamp[$i] = date("M-D-Y", mktime(0, 0, 0, "$month", "$dayNum", "$year"));

It just returns the same date that's not in my list.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
waffeAuthor Commented:
hey wait... its not returning a time stamp... it just returns the date in MMDDYY.
0
 
DrDamnitCommented:
You would need to convert the entire array. Then use array_sort().

Once the array was sorted, you would loop through it a second time, converting it back into the date format you want.
0
 
DrDamnitCommented:
0
 
waffeAuthor Commented:
The array_sort() is getting me somewhere but the mktime does not produce a Unix time stamp?
0
 
Beverley PortlockCommented:
Show us what your dates look like because that is the cause f the problem. Post up a few lines from the print_r($ISOSDates);
0
 
Beverley PortlockCommented:
Additionally, I would like to see the format of the data in $fullDate as well
0
 
Ray PaseurCommented:
A data-dependent date dilemma - lots of D's in there!

General guidelines for dates - store and carry all internal representations in ISO8601 format.  In PHP this is date('c') or date('Y-m-d\TH:i:s').  When you want to print a different kind of date, use strtotime() to convert the internal representation to the formatted date for printing.  The advantage of the ISO format includes correct sort order, easy to line up in columns, looks right in MySQL data base, etc.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.