从阵列中选择下一个即将到来的日期 [英] Select from array next upcoming date

查看:85
本文介绍了从阵列中选择下一个即将到来的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想要实现的是从数组中获取下一个即将到来的日期。在下面,我已经发布了数组以及如何选择当前日期。

Okay essentially what I'm trying to achieve is grab the next upcoming date from the array. Below I have posted my array and how I select the current date.

$payout_dates = array('16-04-14','16-04-28','16-05-14');
$currentdate = date('y-m-d');

我要做的是使用<$ c从数组中选择下一个即将到来的日期$ c>当前日期,例如,今天是 16-04-27 ,因此下一个即将到来的日期将是 16- 04-28 下面,我将粘贴代码im用来选择下一个即将到来的日期,但是更改下面代码的唯一问题是,它用于 select ,所以在使用它来回显日期本身时会遇到麻烦。

What I'm trying to do is select the next upcoming date from the array using the current date so for example today is 16-04-27 so the next upcoming date would be 16-04-28 Below I'm going to paste the code im using to select the next upcoming date however the only problem with changing the code below is that it is used for a select so having troubles just using it to echo out the date itself.

<?php
   foreach ($payout_dates as $i => $d) {
    if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
        $selected = "selected";
        $selected_int = $i;
    } else {
        $selected = "";
    }
    list($year, $month, $day) = explode('-', $d);
    echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
 }
?>

只是为了重提我遇到的问题,就是从当前日期选择下一个即将到来的日期。

Just to reliterate the problem I'm having is selecting the next upcoming date from the current date.

推荐答案

$payout_dates = array('16-04-14','16-04-28','16-05-14');
$currentdate = date('y-m-d');

如果您不想使用 strtotime()转换日期,因为您对日期有字典顺序,以下可能会使用字符串比较来选择日期。

If you don't want to convert dates using strtotime() the following might be used to select the date using string comparison since you have a lexicographical order on the dates.

$found = false;
foreach ($payout_dates as $i => $d) {
    if (! $found && strcmp($currentdate, $d) < 0) {
        $found = true;
        $selected = ' selected="selected"';
    } else {
        $selected = "";
    }
}
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";

否则,您将采用以下解决方案:

Otherwise, you would go with the following solution:

$payout_times = array_map('strtotime', $payout_dates);
$currenttime = strtotime(date('y-m-d'));

$found = false;
foreach ($payout_times as $i => $d) {
    if (! $found && strcmp($currenttime, $d) < 0) {
        $found = true;
        $selected = ' selected="selected"';
    } else {
        $selected = "";
    }
}
echo "<option $selected>" . date("m/d/Y", $d) . "</option>";

这篇关于从阵列中选择下一个即将到来的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆