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:
Live example:
http://kcnovabeseda.cz/uvodni-strana...
Thank You!
I will appreciate any ideas.
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.
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:
Do You have any idea?
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:
Do You have any idea?
You can skip them in the foreach loop like so:
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).
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).
That's works almost perfectly, but now I can't limit number of displayed event's correctly:
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? :)
$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? :)
EDIT: Oops nevermind.
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.
If anyone else has a better solution I'm all ears :)
$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 :)
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:
would work?
I'm guessing:
$pl->filterByAttribute('date', date('Y-m-d'), '>=');
would work?
Thank You a lot :) now it's working exactly how I wanted to. Solved.
If you can't access it then you can sort the array of pages by: