Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 511
  • Last Modified:

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

0
waffe
Asked:
waffe
  • 4
  • 3
  • 3
  • +1
3 Solutions
 
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
 
waffeAuthor Commented:
hey wait... its not returning a time stamp... it just returns the date in MMDDYY.
0
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!

 
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
 
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

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now