Sorting pagelist - from closest date to most far date

Permalink 2 users found helpful
Hi,
I need pagelist which is sorted by date. It must be sorted from closest date (which is set in customAttribute) to most far date.

For example, I have:
20th of September, 20:00
10th of September, 21:00
9th of September, 20:00
10th of September, 20:00

I need to sort it like this (if today is 9th of September):
9th of September, 20:00
10th of September, 20:00
10th of September, 21:00
20th of September, 20:00

I already have this:
foreach( $pages AS $page){
$dateString = $page->getAttribute('date'); //that's the date&time of event (format YYYY-MM-DD HH-MM-SS)
if(strtotime($dateString) > time()) {
# event will begin in future, display event
}
if(strtotime($dateString) < time()) {
# it's already after the event, display nothing
}
if(strtotime($dateString) == time()) {
 # if it's today i need to display TODAY, but this is working also with time, that's not what I want
print 'TODAY';
}
}


Live example:
http://kcnovabeseda.cz/uvodni-strana...


Thank You!
I will appreciate any ideas.

 
beebs93 replied on at Permalink Reply
beebs93
When the page list is being made you could easily sort by attributes:

$pl->sortBy('ak_date', 'asc');


If you can't access it then you can sort the array of pages by:

usort($pages, create_function('$a, $b', '
   return strtotime($a->getAttribute("date")) - strtotime($b->getAttribute("date"))
'));
foreach($pages as $page){
// blah blah blah
}
northon replied on at Permalink Reply
Thank You, it's working and it's easier than I thought. Now I have page list sorted by date, but old, passed event's are showing, how to hide it?

Example: if today is 11th of September:
7th September '11 - this event is passed, don't show in pagelist anymore
8th September '11- this event is passed, don't show in pagelist anymore
11th September '11
13th September '11

I tried to do it this way, but I don't know how to continue:
$dateString = $page->getAttribute('date');   
   if(strtotime($dateString) < time()) {
   # event is already passed, don't show it anymore..
}


Do You have any idea?
beebs93 replied on at Permalink Reply
beebs93
You can skip them in the foreach loop like so:

foreach($pages as $page){
   $dateString = $page->getAttribute('date');
   if(strtotime($dateString) < time()) continue;
   // blah blah blah
}


You could also use the $pl->filterByAttribute() method, but in this case it depends on how your date is stored (a timestamp, a formatted string, etc).
northon replied on at Permalink Reply
That's works almost perfectly, but now I can't limit number of displayed event's correctly:
$pages = $pl->get($itemsToGet = 3, $offset = 0);

It will display only 1 event, becouse 2 another are already passed. So they must be uncounted somewhere in $itemsToGet, I guess. Could You tell me please, how to do that? :)
mkly replied on at Permalink Reply
mkly
EDIT: Oops nevermind.
beebs93 replied on at Permalink Reply
beebs93
I'd vote for getting ALL pages with that "date" attribute and building an array with 3 valid pages with current or future dates to keep things simple.

$pl->filterByAttribute('date', '', '!=');
$pl->sortBy('ak_date', 'asc');
$pages = $pl->get();
$pages = array_slice(array_filter($pages, create_function('$v', 'return strtotime($v->getAttribute("date")) >= time();')), 0, 3);
foreach($pages as $page){
   // blah blah blah
}


If anyone else has a better solution I'm all ears :)
beebs93 replied on at Permalink Best Answer Reply
beebs93
I took another look at the database tables the $pl->filterByPublicDate() method and I forgot that the date/time page attribute values are stored in the DATETIME data type so in theory you could compare date strings.

I'm guessing:
$pl->filterByAttribute('date', date('Y-m-d'), '>=');

would work?
northon replied on at Permalink Reply
Thank You a lot :) now it's working exactly how I wanted to. Solved.