PHP - Foreach和比较日期

时间:2017-10-29 20:16:47

标签: php date foreach

您好我已经预测了数据库中事件的数据,我在数据库中使用日期一个名称,例如event_date。我需要比较相同的日期并在一个div中输出。

例如我有这个事件。

  

第一场比赛 - 9月13日

     

第二场比赛 - 9月1日

     

第三场比赛 - 9月13日

我需要输出

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

这是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;
}
    ?>

这不起作用,是否有某种方式可以通过这种方式进行比较?

谢谢(对不起我的英文)

修改 我使用ACF专业版,我在一个div中需要相同的事件输出并创建类似日历的东西。所以这是真正的代码

<?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: 最终输出

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

EDIT3(已解决):

$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;

我在Matthew Boynes的不同页面上找到了这个解决方案:)

1 个答案:

答案 0 :(得分:0)

您可以使用usort并创建自己的自定义函数来获取结果并过滤它们。

<?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);

您的输出应为:

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
        )

)

我从this answer获得了此功能。回答,Ferdinand Beyer

希望这有帮助