将 UNIX 时间戳的 PHP 数组显示为特定格式的字符串

发布于 2024-11-28 17:09:56 字数 779 浏览 1 评论 0原文

我目前有这段代码:

$dates = array();
foreach($data['sc_event_dates'] as $date) {
    if($date > time()) {
        $dates[] = date( empty($dates) ? "D d M": "d M", $date);
    }
}
echo implode(", ", $dates);

它显示如下内容:

Thu 11 Aug, 18 Aug, 25 Aug, 01 Sep, 08 Sep, 15 Sep

但是我需要对每个月的日期进行分组,以便输出如下所示:

Thu 11, 18, 25 Aug, 01, 08, 15 Sep

$data['sc_event_dates'] 持有一个数组unix 时间戳并按升序顺序排序。

此外,当前时间之前的日期也需要被忽略。

以下是一些示例数据:

Array
(
    [0] => 1313020800
    [1] => 1313625600
    [2] => 1314230400
    [3] => 1314835200
    [4] => 1315440000
    [5] => 1316044800
)

任何人都可以帮助更改我的代码或生成新代码以获得我想要的输出吗?

I currently have this code:

$dates = array();
foreach($data['sc_event_dates'] as $date) {
    if($date > time()) {
        $dates[] = date( empty($dates) ? "D d M": "d M", $date);
    }
}
echo implode(", ", $dates);

And it displays something like this:

Thu 11 Aug, 18 Aug, 25 Aug, 01 Sep, 08 Sep, 15 Sep

But what I need to do group the dates of each month so the output would look like this:

Thu 11, 18, 25 Aug, 01, 08, 15 Sep

$data['sc_event_dates'] holds an array of unix timestamps and is ordered in ascending order.

Also dates that are before current time need to be ignored.

Here is some example data:

Array
(
    [0] => 1313020800
    [1] => 1313625600
    [2] => 1314230400
    [3] => 1314835200
    [4] => 1315440000
    [5] => 1316044800
)

Can any one help alter my code, or produce new code, to get my desired output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-12-05 17:09:56
$dates = array();
foreach($data['sc_event_dates'] as $key => $date) {
    if($date > time()) {

        $next = ++$key;
        $format = 'd';

        if( empty($dates) ) {

            $format = 'D '.$format;
        }

        if( !isset( $data['sc_event_dates'][$next] ) || date('n', $date) != date('n', $data['sc_event_dates'][$next]) ) {

            $format .= ' M';
        }

        $dates[] = date( $format, $date);
    }
}
echo implode(", ", $dates);
$dates = array();
foreach($data['sc_event_dates'] as $key => $date) {
    if($date > time()) {

        $next = ++$key;
        $format = 'd';

        if( empty($dates) ) {

            $format = 'D '.$format;
        }

        if( !isset( $data['sc_event_dates'][$next] ) || date('n', $date) != date('n', $data['sc_event_dates'][$next]) ) {

            $format .= ' M';
        }

        $dates[] = date( $format, $date);
    }
}
echo implode(", ", $dates);
深居我梦 2024-12-05 17:09:56

您需要一个简单的状态机来跟踪正在显示的月份并根据需要调整输出。

$prev_month = null;
foreach(... as $date) {
   if ($date < time()) {
      continue;
   }
   $day = date('D', $date);
   $day_num = date('d', $date);
   $month = date('M');
   if ($prev_month != $month) {
       echo $month, ' ';
       $prev_month = $month;
       $sep = '';
   }
   echo $day_num, $sep;
   $sep = ', ';
}

未测试,YMMV 等...

You'd a simple state machine to keep track of which month is being displayed and adjust output as need be.

$prev_month = null;
foreach(... as $date) {
   if ($date < time()) {
      continue;
   }
   $day = date('D', $date);
   $day_num = date('d', $date);
   $month = date('M');
   if ($prev_month != $month) {
       echo $month, ' ';
       $prev_month = $month;
       $sep = '';
   }
   echo $day_num, $sep;
   $sep = ', ';
}

not tested, YMMV, etc...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文