croakingtoad Posted March 4, 2008 Share Posted March 4, 2008 I am starting with three chunks of code wrapped in a <li> and want to order it based on a date in a variable. Here's what I have for variables in a separate file that is called in with require_once() - $hkLongDate = "02/25/2008"; $vtLongDate = "02/27/2008"; $totLongDate = "02/29/2008"; The code that I want to sort based on the above date is basically three <li> items like below - $hkList = "<li>Item Blue</li>"; $vtList = "<li>Item Orange</li>"; $totList = "<li>Item Green</li>"; I'm just not sure of the best way to associate the var from the first list with the var from the second list and then have it sort by the date value in the first var. Advice? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 4, 2008 Share Posted March 4, 2008 use strtotime just an example: <?php $hkLongDate = strtotime("02/25/2008"); $vtLongDate = strtotime("02/27/2008"); $totLongDate = strtotime("02/29/2008"); if($hkLongDate > $vtLongDate){ $hkList = "<li>Item Blue</li>"; }elseif($vtLongDate > $hkLongDate){ $vtList = "<li>Item Orange</li>"; }else{ $totList = "<li>Item Green</li>"; } ?> Quote Link to comment Share on other sites More sharing options...
croakingtoad Posted March 4, 2008 Author Share Posted March 4, 2008 That's a good start but I need some logic in there to determine which was is the newest and order them to oldest. An if/else strategy may not be the most efficient way to do that? Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 4, 2008 Share Posted March 4, 2008 strtotime returns a timestamp integer Timestamps can be sorted greatest to least for latest to oldest and vice versa. Put the results in an array and use sort() Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 4, 2008 Share Posted March 4, 2008 <?php $hkLongDate = strtotime("02/25/2008"); $vtLongDate = strtotime("02/27/2008"); $totLongDate = strtotime("02/29/2008"); $now = strtotime("now"); // Compaire your strings agains the $now variable ?> Quote Link to comment Share on other sites More sharing options...
obsidian Posted March 4, 2008 Share Posted March 4, 2008 Step #1: Change your dates to UNIX timestamps: <?php $hkLongDate = strtotime("02/25/2008"); $vtLongDate = strtotime("02/27/2008"); $totLongDate = strtotime("02/29/2008"); ?> Step #2: Create an associative array to tie the elements to their respective times: <?php $eles = array( array('time' => $hkLongDate, 'item' => $hkList), array('time' => $vtLongDate, 'item' => $vtList), array('time' => $totLongDate, 'item' => $totList) ); ?> Step #3: Sort the list based on the time and output: <?php // Get the columns for multisort foreach ($eles as $key => $row) { $times[$key] = $row['time']; $items[$key] = $row['item']; } array_multisort($times, SORT_ASC, $items, SORT_ASC, $eles); // Check the order: echo "<pre>\n"; print_r($eles); echo "</pre>\n"; ?> Quote Link to comment Share on other sites More sharing options...
croakingtoad Posted March 5, 2008 Author Share Posted March 5, 2008 Awesome, thanks for the help! SOLVED Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.