ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Display entries by current month from Date Field

September 10, 2011 9:56am

Subscribe [2]
  • #1 / Sep 10, 2011 9:56am

    richardmeredith

    48 posts

    Hi, I’m wondering if it’s possible to display entries by current month from a date channel field.

    Basically they are adding about 2 months worth of data at once so can’t go by the publish date.

    Fingers crossed!

    R

  • #2 / Sep 11, 2011 12:33am

    LMO

    203 posts

    Here is a sample php script that will grab the entry_ids for the current month of a custom field. You will need to find the custom field_id of the date field. Set PHP to input and add the field_id and channel_id to the variables at the top.

    <?php
        
        $channel = 3;
        $field = 'field_id_5';
        
        function implode_r($glue, $pieces)
        {
            $return = "";
    
            if(!is_array($glue)){
                $glue = array($glue);
            }
            
            $thisLevelGlue = array_shift($glue);
    
            if(!count($glue)) $glue = array($thisLevelGlue);
            
            if(!is_array($pieces)){
                return (string) $pieces;
            }
            
            foreach($pieces as $sub){
                $return .= implode_r($glue, $sub) . $thisLevelGlue;
            }
    
            if(count($pieces)) $return = substr($return, 0, strlen($return) -strlen($thisLevelGlue));
    
            return $return;
        }
        
        $this->EE =& get_instance();
        
        $month_start = mktime(0, 0, 0, date("m")  , 1, date("Y"));;
        $month_end = mktime(0, 0, 0, date("m")+1  , 1, date("Y"));;
        
        $results = $this->EE->db->select('titles.entry_id')
                    ->from('exp_channel_titles as titles')
                    ->join('exp_channel_data as data', 'titles.entry_id = data.entry_id')
                    ->where("$field >= $month_start")
                    ->where("$field < $month_end")
                    ->where('titles.channel_id',$channel)
                    ->get()
                    ->result_array();
                            
        $ids = implode_r("|", $results);
    
    ?>

    Then just add a simple echo to the entries loop.

    {exp:channel:entries 
        channel="data" 
        entry_id="<?php echo $ids; ?>"
        }
  • #3 / Sep 11, 2011 9:45am

    richardmeredith

    48 posts

    Thanks!

    Ended up going with Solspace Calendar to display the events but will give this a whirl also.

    Thanks again, much appreciated 😊

    Rich

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases