Jump to content

Get pages by datefield

Recommended Posts

Hi all,

I have some pages with a custom datefield who stores the birth of the person. I want to show a list with the birthdays of the current month but i don't now how to get all the pages that match the condition.

I know that this doesn't work but it reflex what i'm trying to achieve:

$today = getdate();
$currentmonth = $today[month]; //november
$people = $pages->get("/people/")->find("birthday~=$currentmonth"); //voila i get all the people who born on november! 

birthday is an custom datefield...

Anybody could spare me a tip? Thanks!

Share this post

Link to post
Share on other sites

This is the post which helped me understand getting pages by date. I think you can work it out!

$start = strtotime( date('Y-m-d') . " 00:00:00");
$end = strtotime( date('Y-m-d') . " 23:59:59");
$items = $pages->find("template=linkmain,publishdate>=$start,publishdate<=$end,sort=-userid");

Share this post

Link to post
Share on other sites

Thanks arjen. Now i understand how to get pages between two dates, but I could not figure out how to delimit a range of days (01 of november to 30 of november) regardless of the year.

My current solution is to get all the people and then evaluate the month using a conditional:

$items = $pages->find("template=people, sort=birthday");
$currentmonth = date("m");

foreach($items as $people){
$birth = $people->birthday;
$birthmonth = strftime("%m", strtotime($birth));
if($birthmonth == $currentmonth){
echo "{$people->title}"; //Gets the name of the person

Share this post

Link to post
Share on other sites

My apologies since I misunderstood you. The way you've done it seems fine to me. You can also create a pageArray:

$happyPeople = new PageArray();
foreach($items as $people) {

Now you manipulate the array like sorting, shuffling or all other stuff using the API.

Not tested, written in browser.

  • Like 1

Share this post

Link to post
Share on other sites

I don't think it's possible with a selector to recieve pages that have one specific month (across years). Your solution is the only I can think of.

However you could write an helper function to just be able to use $pages->findPeopleBorn(9); to keep template code thin. A module with a hook $this->addHook("Pages::findPeopleBorn",$this,"findPeopleBorn"); which then has a public method findPeopleBorn($event) that returns results would be a good way to do this. See "HelloWorld.module" for example.

One way to archive it without going through all users would be to separate day,month,year into their own fields. This would be easy to then query all pages with a specific month via a simple find(selector).

Or a custom mysql sql query to recieve the id's and load the pages from that. Assuming the date field is called "birthdate":

$tmplID = $templates->get("people")->id;
$month = 8;
$query = "
   SELECT field_birthdate.data,field_birthdate.pages_id
   FROM field_birthdate LEFT JOIN pages ON (field_birthdate.pages_id=pages.id)
   WHERE (EXTRACT(MONTH FROM field_birthdate.data) = $month)
   AND pages.status < 2048 AND pages.templates_id=$tmplID
   ORDER BY pages_id";

$res = $db->query($query);
   $ids = array();
   while($u = $res->fetch_array()) $ids[] = $u['pages_id'];

$ids = implode("|",$ids);
$people = $pages->find("id=$ids");
foreach($people as $p){
   echo "<p>$p->title</p>";
  • Like 4

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Elchin
      I want select pages where now between date and end_date or now bigger than date and end_date is empty.
      I have five tried variants:
      $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("foo=(date<$start,date_end=''),bar=(date<$start,date_end>=$start),sort=-date,limit=12"); $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("date<$start,(date_end='',date_end>=$start),sort=-date,limit=12"); $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("date_end=''|date_end>=$start,date<$start,sort=-date,limit=12"); $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("!date_end|date_end>=$start,date<$start,sort=-date,limit=12"); $start = strtotime(date('Y-m-d') . " 00:00:00"); $results = $page->children("date_end>=$start|!date_end,date<$start,sort=-date,limit=12"); All this variants not worked for me and returned zero results.
    • By MateThemes
      Hello everyone.
      I have a question that i can't find a way to solve.
      I have following function in _uikit.php 
      $date = $page->get('date|createdStr'); $dateModified = $page->get('datemodified'); But I need to output the $date in to different formats.
      My further function looks like this
      // return the blog post article markup return " <div> <article class='uk-article blog-post $class'> <meta property='name' content='$page->title'> <meta property='author' typeof='Person' content='Arra Lifte Harmanschlag'> <meta property='dateModified' content='$dateModified'> <meta property='datePublished' content='$date'> <meta class='uk-margin-remove-adjacent' property='articleSection' content='News'> <div property='image' typeof='ImageObject'> $featuredBlogPostImage </div> $heading <ul class='mt25 uk-margin-remove-bottom uk-subnav uk-subnav-divider'> <li class='uk-article-meta'> <time datetime='$date'>$byline</time> </li> </ul> <div class='mt25' property='text'> $body </div> </article> </div> "; Now I need to output the meta property in this format 2019-03-02CET05:23:00 and then a normal date format that is displayed on the Homepage with 2. März 2019 without time.
      Can anybody help me?
      Thanks in advance.
    • By Eunico Cornelius
      I am trying to get the strftime format from the function convertDateFormat(). However, the output has $ signs in it which i cannot use in strftime.(example of conversion : $d/$m/%Y what strftime actually uses: %d/%m/%Y). Am I in the wrong here or is there some way to format strftime? Thank you in advance :).
    • By ksymmons
      I'm creating a multilingual blog for a client. I'm using Multi-language fields, and it's working great. Essentially, there are two versions of each article: one in English and one in Spanish. 
      Now, I've created a Datetime field with a "Date/time picker" so that they can add a publication date (no time) to their blog entries. I want the month part of the date to be displayed in whatever language the article is being displayed in. Let's take the following example:
      1) "Un artículo de ejemplo", accessible via example.com/un-articulo-de-ejemplo/
      2) "A sample article", accessible via example.com/en/a-sample-article/
      In this example, for article number 1 I want the date to be "12 ene 2017", and for article number 2 I want it to be "12 jan 2017". In other words, the same format for both languages, but localised. And for some reason, I seem unable to do so and dates are always displayed in English.
      Here's the code I'm using to display the date field:
      <?= $post->publication_date; ?> How can I get this to work? Thanks.

    • By fbg13
      When echoing a date field directly from the $user variable
      echo $user->date;  the timestamp is printed. Using
      echo $users->get($user->id)->date; prints the correct formated value.
      Is this a bug or or intended?
  • Create New...