ndolph Posted January 14, 2015 Share Posted January 14, 2015 This is my code... function events_carousel() { $events = wire()->pages->get("/events/")->children('start=0, event_date>=' . date("Ymd") . ', limit=5, sort=-created'); foreach ($events as $event) { echo "<li><strong>" . $event->title."</strong> - ".$event->event_date . "</li>"; } } And this is the output.. Back to the future! - 20141217 MORE MULTI EVENT DAYS - 20150121 Testing multi day events - 20150121 Whisper does things - 20150126 Crazy dog stuff - 20150121 But, it shouldn't display "Back to the future!", but it doesn't seem to care about the event_date. Where am I going wrong? Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 14, 2015 Share Posted January 14, 2015 I would suggest you to use a standartized dateformat, just to be sure the date is parsed correctly. date_start>=today date_start>=1365436783date_start>=2013-04-08date_start>=4/8/2013date_start>=8.4.2013date_start>="April 8, 2013" From the comments here: http://processwire.com/api/selectors/#examples 1 Link to comment Share on other sites More sharing options...
ndolph Posted January 14, 2015 Author Share Posted January 14, 2015 I would suggest you to use a standartized dateformat, just to be sure the date is parsed correctly. From the comments here: http://processwire.com/api/selectors/#examples I was using a standardized date format before and it didn't work for some reason. Changed the field to use l, F j, Y g:i a and changed the code to the following.. $events = wire()->pages->get("/events/")->children('start=0, event_date>=' . date("l F j Y g:i a") . ', limit=5, sort=-created'); It now works. *thumbs up* Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 14, 2015 Share Posted January 14, 2015 You don't need to change the settings of your field. The date is stored as timestamp anyway. Only the date in the selector must be "readable" for php, so it can be compared to it. Link to comment Share on other sites More sharing options...
ndolph Posted January 14, 2015 Author Share Posted January 14, 2015 I've reset the settings of the field and have settled with this, I like displaying the date in a different format for certain places that this is embedded... function events_list($location) { $dateFormatOut = 'D M j Y'; $dateSelectorFormat = 'l F j Y g:i a'; $dateQueryComparison = ''; $outString = '<li><strong>%s</strong> - %s</li>'; // If you need to change this for a specific case, do so in the switch below. %s is printf() string representation // Switch to alter options depending on location switch($location) { case 'carousel': $dateFormatOut = 'l F j Y g:i a'; $dateQueryComparison = '>='; break; case 'past': $dateQueryComparison = '<='; break; case 'upcoming': $dateQueryComparison = '>='; break; } $events = wire()->pages->get('/events/')->children(sprintf('start=0, event_date%s%s, limit=5, sort=-created', $dateQueryComparison, date($dateSelectorFormat))); foreach ($events as $event) { printf($outString, $event->title, date($dateFormatOut, $event->event_date)); } } Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 14, 2015 Share Posted January 14, 2015 As you're not using specific dates for your selector, you could also use "today" or "time()" to compare to your datefield. No need to use a string date format, if you use it only in the selector. And you save all the lines about the $selectorFormat. Also I would suggest to change the line in the foreach to this: printf($outString, $event->title, date($dateFormatOut, $event->getUnformatted("event_date"))); This way you'll always get the timestamp of the field, no matter how it's displayed in the backend by the settings. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now