Jump to content

how can i find/get all items in a current year?


maxf5
 Share

Recommended Posts

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

Link to comment
Share on other sites

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");

 

  • Like 3
Link to comment
Share on other sites

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");

 

  • Like 7
Link to comment
Share on other sites

Wow, thank you all for the nice and quick help! the pw community is unbelievable! :rolleyes:

this works perfectly for me:

<?php

$startdate = date('Y') . "-01-01";
$enddate   = date('Y') . "-12-31";
$zaehler = $page->children("einsatzdatum>=$startdate, einsatzdatum<=$enddate");
  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...