0

Using Advanced Custom Fields with the add-on of Date Time Piker, I populate a calendar with the information of Year, Date, Time (am and pm).

On the calendar listing page, I have a query and, for some reason, it skips February and repeats March twice.

Here is the code:

<?php
                $today = date('Ymd h:i:s a', time() - 60 * 60 * 24);

                    #start from current month. Change 2 to however months ahead you want
                    for ($x=0; $x<=6; $x++) {

                        $date = new DateTime("$x months");
                        $date->modify("-" . ($date->format('j')-1) . " days");
                        #echo $date->format('j, m Y');  
                        $month =  $date->format('m');
                        $year =  $date->format('Y');    
                        #echo 'Month= '.$month .' Year= '.$year.' <br>'; #debug

                        $rows = $wpdb->get_results($wpdb->prepare( 
                            "
                            SELECT * 
                            FROM wp_postmeta
                            WHERE meta_key LIKE %s
                                AND meta_value LIKE %s
                                ORDER BY meta_value ASC
                            ",
                            'show_date_time_%_show_date', // meta_name: $ParentName_$RowNumber_$ChildName
                            #''.$year.''.$month.'%' // meta_value: 20131031 for example
                            ''.$year.''.$month.'%' // meta_value: 20131031 for example
                        ));

                        // loop through the results
                        if( $rows ) {
                            echo '<div class="month">';
                            echo '<h2>'.$date->format('F').' '.$date->format('Y').'</h2>';
                            echo '<ul>';
                            foreach( $rows as $row ) {
                                // for each result, find the 'repeater row number' and use it to load the sub field!
                                preg_match('_([0-9]+)_', $row->meta_key, $matches);
                                $meta_key = 'show_date_time_' . $matches[0] . '_show_date'; // $matches[0] contains the row number!

                                // today or later
                                $showDate = $row->meta_value;
                                $do_current = ($showDate > $today); 
                                if ( $do_current || $continue ) :

                                    $show_title = get_the_title( $row->post_id );
                                    $machine_name = preg_replace('@[^a-z0-9-]+@','-', strtolower($show_title)); 


                                    $post_type = get_post_type($row->post_id);
                                    if( $post_type === 'pre-post-show' ){
                                        // echo 'pre-post-show matching';
                                        $postID = $row->post_id;
                                        $posts = get_field('pre_post_related_show', $postID);

                                        if( $posts ){ 

                                            foreach( $posts as $post): // variable must be called $post (IMPORTANT)
                                                setup_postdata($post); 
                                                $related_show = get_the_title($post->ID);
                                                $related_show_machine = preg_replace('@[^a-z0-9-]+@','-', strtolower($related_show));
                                            endforeach;

                                            wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                                        }

                                    }// post type define
                                ?>

Any thoughts about why this could be happening?

j08691
  • 204,283
  • 31
  • 260
  • 272

1 Answers1

0

Well, it's a bit trickier than it seems. In this stackoverflow post you'll find a complete explanation of this "odd" behaviour. To sum up:

  1. +1 month increases the month number (originally 1) by one. This makes the date 2015-02-31.
  2. The second month (February) only has 28 days in 2015, so PHP auto-corrects this by just continuing to count days from February 1st. You then end up at March 3rd.

That's why you're seeing month 3 twice. How to get around this? Well, in the post I showed you there are some ways of getting around this "problem". If you have php 5.3 or higher, I'd suggest this:

$date = new DateTime();// Now.
for ($x=0; $x<=6; $x++) {
    ($x) ? $date->modify( 'first day of next month' ) : NULL;// It should give you next month from $x=0 onwards. Output: 01,02,03,04,05,06,07
    ....// Your code comes here

Hope it helps.

UPDATE

There are a lot of documentation relative to dates and PHP. The DateTime class is really handy to deal with dates. Check its modify method to change the date. Lastly, have a look at the different date and time supported formats. Especially to the relative formats if you're interested in modifying dates using strings like '+7 day'.

Lastly, have a look at the wpdb documentation. The class wpdb allows you to interact with the database in wp. You'll have to learn a bit of SQL in order to make queries. There are hundreds of pages on the internet (not to mention books) that will help you to learn.

For instance, if you want to retrieve all posts that are within the next 7 days you could do something like:

$now_date = (new DateTime())->format("Y-m-d");
$next_seven_days = (new DateTime("+7 day"))->format("Y-m-d");
$query = "SELECT * FROM wp_posts WHERE post_date >= '$now_date' AND post_date < '$next_seven_days'";
$rows = $wpdb->get_results( $query, OBJECT );

Note: since there is no input from the users/visitors nor they can't effect the query, I'm not going to use prepare for simplicity's sake. Anyway, I must say it's good practice to use it systematically.

Community
  • 1
  • 1
acontell
  • 6,792
  • 1
  • 19
  • 32
  • Thank you! This was very useful. Are there any resources where I can learn more about creating the date range pull? I got this from a colleague and would like to figure out how I can say, for instance: Pull all posts that are within the next 7 days. – Nicholas Peterson Jan 31 '15 at 00:34