0

Hello I have foreach where I geting data of events in database, I use for date one name in database for example event_date. I need compare action with same date and output in one div.

For example I have this events.

Event one - 13 Sep

Event two - 01 Sep

Event three - 13 Sep

Output i need

<div class="event_head">
        <span>Event one</span>
        <span>13 Sep</span>
        <br/>
        <span>Event three</span>
        <span>13 Sep</span>
</div>

This is condition in foreach

if($date == $date){
    ?>

    <div class="event_head">
        <span><?php echo $name_event?></span>
        <span><?php echo $date;?></span>
        <br/>
    </div>

    <?php

}else{
    echo $date;
}
    ?>

This is not working, is some way how to compare it some this way ?

Thank you (sorry for my english)

EDIT: I use ACF pro version and I need same event output in one div and create something like calendar. So here is real code

<?php 

$posts = get_posts(array(
    'post_type' => array('town1_event', 'town2_event'),
    'meta_key'  => 'datum_eventu',
    'orderby'   => 'meta_value_num',
    'order'     => 'ASC'
));

if( $posts ) {
    
    foreach( $posts as $post ) {

        //$datum = get_field("datum_eventu");
        //$nazev = get_field("nazev_eventu");

        $date = get_field('datum_eventu', false, false);    
        $date = new DateTime($date);

        //echo $date->format('j M');
        //echo $date->format('j M Y')

        //echo $datum;

        ?>
        
        <?php
        if($date->format('jM') == $date->format('jM')){
        ?>
        <div class="calendar_head">
        <span><?php echo $nazev?></span><br/>
        <span><?php echo $date->format('jM');?></span>
        </div>
        <?php   
        }else{
        echo "<br/>";
        echo $datum;
        }
    }

    wp_reset_postdata();
    
}
?>

EDIT2: Final output

22 JUL              | 25 JUL
EVENT 1     EVENT 2 | EVENT 3

EDIT3 (Solved):

$the_query = new WP_Query( array(
    'post_type'   => array('town1_event','town2_event'),
    'post_status' => 'publish',
    'meta_key'    => 'datum_eventu',
    'orderby'     => 'meta_value_num',
    'order'     => 'ASC'
) );

# This will hold what group we're in
$current_header = '';

# The Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post();

    # get the datum for this post
    $temp_date = get_post_meta( get_the_ID(), 'datum_eventu', true );

    # If they aren't the same, we'll start a new group, which for now
    # just means setting a new heading
    if ( $temp_date != $current_header ) {
        $current_header = $temp_date;
        $old_date = date($current_header);             
        $old_date_timestamp = strtotime($old_date);
        $new_date = date('d. M', $old_date_timestamp); 
        echo "<h2>$new_date</h2>";

    }
    $nazev = get_field("nazev_eventu");
        echo $nazev;
        echo "<br/>";
    # ... do normal loop stuff here

endwhile;

I find this solution on different page from Matthew Boynes :)

Community
  • 1
  • 1
Tomvo
  • 33
  • 1
  • 6
  • 1
    sort by date in the querry, then keep track of date changes in the code –  Oct 29 '17 at 20:18
  • 2
    Your logic for `$date == $date` makes no sense; it's always going to be true; it's like saying `1 == 1`. So really it would not change anything. – Sam Oct 29 '17 at 20:22
  • @nogad Hello I have it order by date, how can I track of date changes, sorry for my stupidity – Tomvo Oct 29 '17 at 20:27
  • @Tomvo are days and months separate? Or is it just one string? What's the content of `$date`? – Sam Oct 29 '17 at 20:29
  • store in temporary variable, check for changes. –  Oct 29 '17 at 20:31
  • @Samuel I use `$date->format('j M')` – Tomvo Oct 29 '17 at 20:32

1 Answers1

0

You can use usort and create your own custom function to get the results and filter them.

<?php    
$events = array(
    'event1' => array(
        'event_name' => 'Title for Event 1',
        'event_date' => '2017-11-1'
    ),
    'event3' => array(
        'event_name' => 'Title for Event 1',
        'event_date' => '2017-10-13'
    ),
    'event4' => array(
        'event_name' => 'Title for Event 1',
        'event_date' => '2017-11-10'
    ),
    'event2' => array(
        'event_name' => 'Title for Event 1',
        'event_date' => '2017-10-22'
    ),
);

function date_compare($a, $b)
{ 
    // note that the variables are calling for the date part of the array
    // if you are using assoc array from mysql just change the value
    // to your row name
    $t1 = strtotime($a['event_date']);
    $t2 = strtotime($b['event_date']);
    return $t1 - $t2;
}    
usort($events, 'date_compare');
print_r($events);

Your output should be:

Array
(
    [0] => Array
        (
            [event_name] => Title for Event 1
            [event_date] => 2017-10-13
        )

    [1] => Array
        (
            [event_name] => Title for Event 1
            [event_date] => 2017-10-22
        )

    [2] => Array
        (
            [event_name] => Title for Event 1
            [event_date] => 2017-11-1
        )

    [3] => Array
        (
            [event_name] => Title for Event 1
            [event_date] => 2017-11-10
        )

)

I got this function from this answer. Answered by, Ferdinand Beyer.

Hope this helps

Sam
  • 2,856
  • 3
  • 18
  • 29
  • Hello thank you for your answer. I add my actual real code but this not working. If you want help me please look at my edit :) thank you very much ! – Tomvo Oct 30 '17 at 17:20
  • @Tomvo where did yo add the function? – Sam Oct 30 '17 at 17:23
  • Also, wha is your `get_field` function? – Sam Oct 30 '17 at 17:24
  • 1
    this function can be used to load the value of any field from any location, returns the value of a specific field. I have define on every page this "field" and this field is filled with output data. When I write `echo $date` I will get all date from posts like "01.09.2017" for example. [link]https://www.advancedcustomfields.com/resources/get_field/ – Tomvo Oct 30 '17 at 18:51
  • I need only get all date with same date in one div because I create something like events list so finnaly I get . 28.01.2017 is Event 1 and Event 2 other 11.11.2017 is Event 3 ... – Tomvo Oct 30 '17 at 18:55
  • Thank you all for help I found a solution. Look my edit if you want :). – Tomvo Oct 31 '17 at 09:20
  • No problem @Tomvo – Sam Oct 31 '17 at 18:38