将 UNIX 时间戳的 PHP 数组显示为特定格式的字符串
我目前有这段代码:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个简单的状态机来跟踪正在显示的月份并根据需要调整输出。
未测试,YMMV 等...
You'd a simple state machine to keep track of which month is being displayed and adjust output as need be.
not tested, YMMV, etc...