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 :)