maxf5 Posted March 10, 2017 Posted March 10, 2017 Hi devs! I have multiple missions on a fire department website. Now i want a counter to show all missions in the current year. I tried: <?php $year = date("Y"); $zaehler = $page->children("einsatzdatum$=$year"); echo "<p>{$zaehler->getTotal()} in {$year}</p>"; ?> the "einsatzdatum" field is a datetime field ( dd.mm.yy / 10.03.2017 ) But i get all the items (8) instead of only the one's in 2017 (5). Best regards, Max
BitPoet Posted March 10, 2017 Posted March 10, 2017 The dd.mm.yy format is what PW outputs, MySQL itself expects a standard datetime literal. Therefore, if you want to get all pages with a date in the current year, you need to search for a minimum date (and perhaps maximum if there are dates in future years present). <?php $startdate = date('Y') . "-01-01"; $enddate = date('Y') . "-12-31"; $zaehler = $page->children("einsatzdatum>=$startdate, einsatzdatum<=$enddate"); Or you could be daring and try out my (still beta) DatetimeAdvanced module that makes use of MySQL's builtin date and time functions. With it, you could say: <?php $year = date("Y"); $zaehler = $page->children("einsatzdatum.year=$year"); 3
Robin S Posted March 10, 2017 Posted March 10, 2017 24 minutes ago, BitPoet said: The dd.mm.yy format is what PW outputs, MySQL itself expects a standard datetime literal. You can also use timestamps in a PageFinder selector or anything that works with strtotime(): $zaehler = $page->children("einsatzdatum>=first day of January 2017, einsatzdatum<=last day of December 2017"); 7
maxf5 Posted March 10, 2017 Author Posted March 10, 2017 Wow, thank you all for the nice and quick help! the pw community is unbelievable! this works perfectly for me: <?php $startdate = date('Y') . "-01-01"; $enddate = date('Y') . "-12-31"; $zaehler = $page->children("einsatzdatum>=$startdate, einsatzdatum<=$enddate"); 1
LostKobrakai Posted March 11, 2017 Posted March 11, 2017 You might want to add hour/minute(/seconds) definitions as well or you might miss some edge cases in comparing those timestamps. It's not only comparing the date part. 1
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