Jump to content

Pull today's events to home page from fullcalendar event page fields


ljones
 Share

Recommended Posts

Thanks to this post, I have integrated sakkoulas' code and have full calendar page working. I want to set up a feed to the homepage to display today's events and link to the event. This is what I have so far on the home page:

 <?php    
           foreach($pages->get("/events/")->children as $child) {
           
             echo "<li><a href='{$child->url}'>" .  $child->date_start . " {$child->title}</a></li>";    
                    }

                    echo "</ul>";
   ?>
 

This code just pulls all of the events and shows the date like this: 2013-06-02 12:00.

I am not sure if this is best practice or not.  But I am stuck on two points: I can't figure out how to override the field default time format for child->date_start  to just echo the hour and minute and I don't know how to select just today's listings.  Any advice would be appreciated. Thanks.

Link to comment
Share on other sites

This solved my date format problem: sort of: the minutes are not rendering correctly. 7:00 shows a s 7:07 and 5:00 shows as 5:05 If I put an %i it just shows an i. %r shows the time fine, but like this: 07:00:00 Maybe it's because I am running it on a local mamp environment and it won't be an issue on my server.

strftime("%l:%I %p", strtotime($child->date_start))
 
Link to comment
Share on other sites

The output formatting of date field you can define in the field settings "Details" and "Input" for in and output format.

To get the timestamp value you have to get around output formatting:

$timestamp = $child->getUnformatted("date_start");

I'm not sure about your problems with strftime. %i for strftime doesn't exist. http://www.php.net/manual/en/function.strftime.php

To get pages from today you'd have to get the 00:00 and 23:59:59 from the day and compare it in the selector.

Or you can use "today" or any strtotime keyword and PW will take it.

$start = strtotime("today");
$end = strtotime("today + 1day");
$pages->get("/events/")->children("date_start>=$start, date_start<$end");

or

$pages->get("/events/")->children("date_start>=today, date_start<tomorrow");
  • Like 2
Link to comment
Share on other sites

Thanks so much for your help Soma.

Since the fullcallendar js  for the big calendar page needs to have start_date and end_date formatted like this, ("Y-m-d H:i"), I have to stick with that on the field formatting in the back end. So that is why I am applying the strtotime formatting to show on the home page.

I was making the false assumption that DATE_FORMAT settings worked in strftime, so thanks for that link.

There is probably a way to use  a fullcalendar json feed to show the info on the homepage, but processwire made more sense to me since I am not very proficient in javascript. 

So I am not sure if the below code is the most elegant solution, but it works and it seem to be easily adapted to provide a range of days, if needed. Below code has been edited to include Soma's fomatting advice.

 <?       
            foreach($pages->get("/events/")->children("date_start>=today, date_start<tomorrow") as $child) {
      
   
             echo "<li><a href='{$child->url}'><p>" . strftime("%l:%M %P", $child->getUnformatted("date_start")) .
              " {$child->title}</p></a></li>";    
            
            }
                     
            echo "</ul>";
            ?>

Link to comment
Share on other sites

You have input (backend) and output (front-end) formatting for date fields.

I think you miss understood what I was trying to say about output formatting:

To get the unformatted timestamp of a datefield you can do

$timestamp = $child->getUnformatted("date_start");

instead of

$timestamp = strtotime($child->date_start);

Which depending on the format the date has, will fail to get the right timestamp.

So a more bullet proof way is to do this in your code:

strftime("%l:%M %P", $child->getUnformatted("date_start"));
  • 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...