将日期分成几个月 [英] Grouping dates into Months in php

查看:103
本文介绍了将日期分成几个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建下面的制作时间以显示每两周一次的日期

Created the make time below to display biweekly dates

<?php
  $date1 = "07/05/2013";
  $date2 = date('M j, Y', strtotime($date1 . " + 14 day"));
  $date3 = date('M j, Y', strtotime($date2 . " + 14 day"));
  $date4 = date('M j, Y', strtotime($date3 . " + 14 day"));
  $date5 = date('M j, Y', strtotime($date4 . " + 14 day"));
  $date6 = date('M j, Y', strtotime($date5 . " + 14 day"));
  $date7 = date('M j, Y', strtotime($date6 . " + 14 day"));
  $date8 = date('M j, Y', strtotime($date7 . " + 14 day"));
  $date9 = date('M j, Y', strtotime($date8 . " + 14 day"));
  $date10 = date('M j, Y', strtotime($date9 . " + 14 day"));
  $date11 = date('M j, Y', strtotime($date10 . " + 14 day"));
  $date12 = date('M j, Y', strtotime($date11 . " + 14 day"));
  $date13 = date('M j, Y', strtotime($date12 . " + 14 day"));
  $date14 = date('M j, Y', strtotime($date13 . " + 14 day"));
  $date15 = date('M j, Y', strtotime($date14 . " + 14 day"));
  $date16 = date('M j, Y', strtotime($date15 . " + 14 day"));
  $date17 = date('M j, Y', strtotime($date16 . " + 14 day"));
  $date18 = date('M j, Y', strtotime($date17 . " + 14 day"));
?>

如何让它按月分组?让我们想知道八月或十二月的日期有多少。另外如果我想在年底前得到多少日期?帮助手将不胜感激。

How can I get it to group together by Month? Let say I want to know how many dates land in August, or December. Also if I wanted to get how many dates till end of the year? Helping hand will be greatly appreciated.

推荐答案

尝试这样:

<?php
// initiate months
$month_arr = Array();
for ($i=1; $i<=12; $i++){
    // no. of dates
    $month_arr[$i] = 0;
}
$date_arr = Array();
$date_start = "07/05/2013";
$date_arr[] = date('M j, Y', strtotime($date_start));

for ($i=1; $i<=17; $i++){
    $date_temp = date('M j, Y', strtotime($date_arr[$i-1] . " + 14 day"));
    $month = date('n', strtotime($date_temp));

    $month_arr[$month] += 1;
    $date_arr[] = $date_temp;

  }

 foreach ($month_arr as $k => $v){
    echo "<BR>Month: " . $k . ", No. of dates: " . $v;
 }

// all dates
echo "<BR>All dates<BR>";
var_dump ($date_arr);
?>

您可以扩展这个逻辑,将实际日期分为几个月,而不是仅将日期计数一个月。这是解决方案:

You can extend this logic to group the actual dates by months rather than getting only the count of dates in a month. This is the solution for that:

<?php
// initiate months
$month_arr = Array(
            'January' => Array('num_dates'=>0, 'dates'=>Array()) , 
            'February' => Array('num_dates'=>0, 'dates'=>Array()), 
            'March' => Array('num_dates'=>0, 'dates'=>Array()), 
            'April' => Array('num_dates'=>0, 'dates'=>Array()), 
            'May' => Array('num_dates'=>0, 'dates'=>Array()), 
            'June' => Array('num_dates'=>0, 'dates'=>Array()), 
            'July' => Array('num_dates'=>0, 'dates'=>Array()), 
            'August' => Array('num_dates'=>0, 'dates'=>Array()), 
            'September' => Array('num_dates'=>0, 'dates'=>Array()), 
            'October' => Array('num_dates'=>0, 'dates'=>Array()), 
            'November' => Array('num_dates'=>0, 'dates'=>Array()), 
            'December' => Array('num_dates'=>0, 'dates'=>Array())
        );
$date_arr = Array();

$date_start = "07/05/2013";
$date_arr[] = date('M j, Y', strtotime($date_start));

for ($i=1; $i<=17; $i++){
    $date_temp = date('M j, Y', strtotime($date_arr[$i-1] . " + 14 day"));
    $month = date('F', strtotime($date_temp));

    $month_arr[$month]['dates'][] = $date_temp;
    $month_arr[$month]['num_dates'] += 1;
    $date_arr[] = $date_temp;
}

foreach ($month_arr as $k => $v){
    if (!empty($v)){
        if ($v['num_dates'] != 0){
            echo "<BR><BR>Month: " . $k;
            echo "<BR>No. of dates: " . $v['num_dates'];
            foreach ($v['dates'] as $k1=>$v1){
                echo "<BR>" . $v1;
            }
        }
     }
}
?>

此时,$ month_arr应该有你需要的一切。

At this point, $month_arr should have everything you need.

这篇关于将日期分成几个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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