Jump to content

Recommended Posts

Posted

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

Posted

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
Posted
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
Posted

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
Posted

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.

  • Like 1

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
  • Recently Browsing   0 members

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