如果下一个日期从当前日期php foreach

时间:2017-08-01 20:06:25

标签: php loops date foreach

所以我试图只为最近的一场音乐会演唱会。如果你愿意,可以“在旁边玩”。

这基本上就是我想做的事情;

if (date is closest to now and in the future ) {
"echo 'active';"
break;
}

这是我到目前为止所得到的:

<?php $tour = get_post_meta($post->ID, 'tourplaces', true);

foreach( $tour as $tour ) {

$now = date('d M Y H:i');
$date_unformated = $tour['tour_when'];
$date_unformated = str_replace('/', '-', $date_unformated);
$date =  date('d M Y H:i', strtotime($date_unformated));


if ($date > $now) {
echo 'active';
} 
?>

但我只希望将来最接近的一个展示。并非所有未来日期都要显示。

4 个答案:

答案 0 :(得分:0)

好的,所以这就是我到目前为止所得到的:

<?php $tours = get_post_meta($post->ID, 'tourplaces', true); 
    if( count($tour) <= 0) { echo "No tours in the future"; }
$min_tour = null;
$min_date = null;
$now = date('d M Y H:i');
 ?>
<?php foreach( $tours as $tour ) { ?>
<?php
$date_unformated = $tour['tour_when'];
$date_unformated = str_replace('/', '-', $date_unformated);
$date =  date('d M Y H:i', strtotime($date_unformated)); ?>
<div class="tourplace <?php if ($date > $now) {
if($min_date == null || $date < $min_date) {
    $min_date = $date;
    $min_tour = $tour;
    echo 'active';
        }
 }?>">

但不幸的是它没有用。它确实显示了未来的日期。我错过了什么吗?

答案 1 :(得分:0)

经过大量的反复尝试后我才开始工作! :-D

最终代码:

<?php $tours = get_post_meta($post->ID, 'tourplaces', true); 
$min_tour = null;
$min_date = null;
date_default_timezone_set('Europe/Stockholm');
$now = date('YmdHi');
 ?>
<?php foreach( $tours as $tour ) { ?>
<?php
$date_unformated = $tour['tour_when'];
$date_unformated = str_replace('/', '-', $date_unformated);
//$date =  date('d M Y H:i', strtotime($date_unformated)); 
$date =  date('YmdHi', strtotime($date_unformated)); ?>
<?php
if($date > $now && ($min_date == null || $date < $min_date)) {
    $min_date = $date;
    $min_tour = $tour;
        } }
 ?>
<?php  foreach( $tours as $tour ) { if ($tour == $min_tour) { ?>
    <div class="tourplace active">
        <address><?php echo $min_tour['tour_location']; ?> </address><br/>
<?php
$date_unformated = $min_tour['tour_when'];
$date_unformated = str_replace('/', '-', $date_unformated);
$date =  date('d M Y H:i', strtotime($date_unformated)); 
 ?>
    <time><?php echo $date; ?></time><br/>
<?php if ($min_tour['tour_book']) { ?>
<a href="<?php echo $min_tour['tour_book']; ?>" target="_blank" class="link btn">Book ticket</a>
<?php } ?>
</div>


    <? } } ?>

答案 2 :(得分:-1)

你想要的是获得最近的日期,所以最小的日期仍然是未来。所以你必须找到最低限度。这是一个位代码。我没有测试它,但它应该说明我想说的话。

<?php 
// renamed $tour to $tours, because it was confusing with the foreach loop
$tours = get_post_meta($post->ID, 'tourplaces', true); 
// in case $tours is an empty array, we echo an info
if( count($tour) <= 0) echo "No tours in the future";
// we want to remember what our closes future tour was
// init to null, as there may be no tour in the future
$min_tour = null;
$min_date = null;
// as  GrumpyCrouton said, $now should be define once outside the loop
$now = date('d M Y H:i');
foreach( $tours as $tour ) {
    $date_unformated = $tour['tour_when'];
    $date_unformated = str_replace('/', '-', $date_unformated);
    $date =  date('d M Y H:i', strtotime($date_unformated));
    if ($date > $now) {
        // check if the current date is before the last found minimal date
        // also check for null in case we havent found a miminmal date yet
        if($min_date == null || $date < $min_date) {
             // our current tour is closer to now and is in the future
             // so save the current tour as minimal
             $min_date = $date;
             $min_tour = $tour;
        }
    }
}
// you have to check for $min_tour == null, in case the was no tour in the future
echo "closest tour:" . $min_tour;

答案 3 :(得分:-2)

你可以使用break;结束foreach。如果您的数组按日期正确排序。

http://php.net/manual/en/control-structures.break.php

 foreach( $tour as $tour ) {

   // your code

    if ($date > $now) {
        echo 'active';
        break;
    }

  }

为了进行比较,请将日期格式更改为:

$date = date ("YmdHi"); 

也适合您的旅行日期。 您将获得一个可以在if语句中进行比较的日期。