在php中将日期分组为月份



创建下面的生成时间以显示每两周的日期

<?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"));
?>

如何按月份分组?比方说,我想知道八月或十二月有多少日期。如果我想在年底前约会多少次呢?我们将不胜感激。

我不确定你关于按月份分组的确切含义但这将使用一个数组来声明所有日期,并且您可以更改$limit变量来增加或减少日期的数量。更改$dates以调整原始日期$left是数组中剩余的天数(它在第一天命中后触发,可以更改为在最后一天触发)$amount是八月的日期

<?php
$limit = 17;
$dates = array("07-05-2013");
for ($i=1; $i<=$limit; $i++){
    $dates[$i]=date('d-m-Y', strtotime($dates[$i-1] . "+ 14 days"));
}
foreach (array_keys($dates) as $key){ 
    $value = date('m', strtotime($dates[$key])); 
    if ($value == "08"){
        $amount = $amount + 1;  
    }
    if ($amount == 1){
        $left = $limit-$key;
    }
}
print_r ($dates);
echo "<br>";
echo $amount . "<br>" . $left;
?>

试试这个:

<?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);
?>

您可以扩展此逻辑,以按月份对实际日期进行分组,而不是只获取一个月中的日期计数。这就是解决方案:

<?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应该拥有您所需要的一切。

只需添加一个if语句并在所有12个月内递增。。。如果月份是相同的

 $n=1;
 $month=array();//dumpyour dates in  an array
 $date=array()
 while($k<30){
 if  (date('n', strtotime($date[$k]))==date('n', strtotime($date[$k-1])){
 echo $date[$k++];
 $m = date('n', strtotime($date[$k]));
 $month[$m]=++$n;
 }
 else{
 .....
 }
 }

由于我不断地对SO进行迭代,PHP的DateTime类使大多数日期时间操作变得微不足道。

你的问题不是100%清楚,但从你的评论来看,你似乎想要一系列时间戳,按月划分,间隔14天。我不知道你是否考虑过这几个月可能会跨年债券,但我在回答时考虑到了这一点。

下面的代码应该做你想做的:-

$start = DateTime::createFromFormat('d/m/Y', '07/05/2013');
$interval = new DateInterval('P14D');
$periods = new DatePeriod($start, $interval, 26);
$dates = array();
foreach($periods as $day){
    /** @var DateTime $day */
    $dates[$day->format('Y')][$day->format('M')][] = $day->getTimestamp();
}
var_dump($dates);

输出:-

array (size=2)
  2013 => 
    array (size=8)
      'May' => 
        array (size=2)
          0 => int 1367936286
          1 => int 1369145886
      'Jun' => 
        array (size=2)
          0 => int 1370355486
          1 => int 1371565086
      'Jul' => 
        array (size=3)
          0 => int 1372774686
          1 => int 1373984286
          2 => int 1375193886
      'Aug' => 
        array (size=2)
          0 => int 1376403486
          1 => int 1377613086
      'Sep' => 
        array (size=2)
          0 => int 1378822686
          1 => int 1380032286
      'Oct' => 
        array (size=2)
          0 => int 1381241886
          1 => int 1382451486
      'Nov' => 
        array (size=2)
          0 => int 1383664686
          1 => int 1384874286
      'Dec' => 
        array (size=3)
          0 => int 1386083886
          1 => int 1387293486
          2 => int 1388503086
  2014 => 
    array (size=5)
      'Jan' => 
        array (size=2)
          0 => int 1389712686
          1 => int 1390922286
      'Feb' => 
        array (size=2)
          0 => int 1392131886
          1 => int 1393341486
      'Mar' => 
        array (size=2)
          0 => int 1394551086
          1 => int 1395760686
      'Apr' => 
        array (size=2)
          0 => int 1396966686
          1 => int 1398176286
      'May' => 
        array (size=1)
          0 => int 1399385886

我建议您使用DateTime对象数组,而不是时间戳数组,然后可以根据需要操作每个对象。在这种情况下,代码看起来是这样的:-

$start = DateTime::createFromFormat('d/m/Y', '07/05/2013');
$interval = new DateInterval('P14D');
$periods = new DatePeriod($start, $interval, 26);
$dates = array();
foreach($periods as $day){
    /** @var DateTime $day */
    $dates[$day->format('Y')][$day->format('M')][] = $day;
}
$months=array("1","2","3","4","5","6","7","8","9","10","11","12");
$years=array('2010','20111','2012','2013','2014','2015','2016','2017','2018');
$dates = array('2015-04-24','2015-04-28','2017-03-24', '2017-03-24', '2017-04-07', '2017-04-14', '2017-04-21', '2017-04-28');
$result=array();
foreach ($months as $mn) {
foreach ($years as $year) {
$r=array();
foreach ($dates as $month) {
if(date_parse_from_format("Y.n.j", $year) 
["year"]==date_parse_from_format("Y.n.j", $month)["year"] && 
date_parse_from_format("Y.n.j", $month)["month"]==$mn){$r[]=$month;}
;}
if(empty($r)){;}else{$result[]=$r;};
;}
;};
var_dump($result);

这段代码应该可以工作,它将根据年份和月份进行分段,请根据需要将更多年份添加到$years变量中,并将您的日期添加到$dates数组中,请注意,重复的值将被消除,如果您希望保留重复的值,只需在将其作为关键字插入数组时,在每个日期的末尾随机添加一个字母,然后在处理完成时删除多余的字母。

相关内容

  • 没有找到相关文章

最新更新