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? Link to comment https://forums.phpfreaks.com/topic/94315-date-based-conditional/ 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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/94315-date-based-conditional/#findComment-483054 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? Link to comment https://forums.phpfreaks.com/topic/94315-date-based-conditional/#findComment-483148 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() Link to comment https://forums.phpfreaks.com/topic/94315-date-based-conditional/#findComment-483157 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 ?> Link to comment https://forums.phpfreaks.com/topic/94315-date-based-conditional/#findComment-483161 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"; ?> Link to comment https://forums.phpfreaks.com/topic/94315-date-based-conditional/#findComment-483224 Share on other sites More sharing options...
croakingtoad Posted March 5, 2008 Author Share Posted March 5, 2008 Awesome, thanks for the help! SOLVED Link to comment https://forums.phpfreaks.com/topic/94315-date-based-conditional/#findComment-483875 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.