在php中将当前月份拆分为几周 [英] Split the current month in to weeks in php

查看:75
本文介绍了在php中将当前月份拆分为几周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将当前月份分为几周,例如第一天到星期六,然后是周日到下一个星期六。例如:对于5月,我想将它分成几周

  2016-05-01至2016-05-07 
2016-05-08至2016-05-14
2016-05-15至2016 -05-21
2016-05-22至2016-05-28
2016-05-29至2016-05-31

如果我尝试以下代码,我没有得到确切的结果。

  <?php 

$ start_date = date('Ym-d',strtotime('2016-06-01'));
$ end_date = date('Y-m-d',strtotime('2016-06-30'));
$ i = 1;
for($ date = $ start_date; $ date< = $ end_date; $ date = date('Ym-d',strtotime($ date。'+ 7 days'))){
echo getWeekDates($ date,$ start_date,$ end_date,$ i);
echo \n;
$ i ++;
}

函数getWeekDates($ date,$ start_date,$ end_date,$ i){
$ week = date('W',strtotime($ date));
$ year = date('Y',strtotime($ date));

$ from = date( Y-m-d,strtotime( {$ year} -W {$ week} +1));
if($ from< $ start_date)$ from = $ start_date;

$ to = date( Y-m-d,strtotime( {$ year} -W {$ week} -7));
if($ to> $ end_date)$ to = $ end_date;

echo $ from。-。$ to。’< br>’;;
}

?>

我喜欢

  2016-05-01-2016-05-01 
2016-05-01-2016-05-08
2016-05-08-2016-05-15
2016-05-15-2016-05-22
2016-05-22-2016-05-29

在for循环中,在检查最后一组日期时,conditon失败。因此它不计算30,31。



我该怎么做?

解决方案

DateTime,DatePeriod和DateInterval是您的朋友,可以快速地做到这一点!



请参见在线



<上课前= lang-php prettyprint-override> <?php
/ **
* @file
* Awnser到http://stackoverflow.com/q / 37224961/392725
*
* @link http://stackoverflow.com/a/372​​28497/392725
* /
date_default_timezone_set('Europe / Paris');

函数getWeekDates(DateTimeInterface $ date,$ format =‘Y-m-d’){
$ dt = \DateTimeImmutable :: createFromMutable($ date);
$ first_day = $ dt-> modify(本月的第一天);
$ last_day = $ dt-> modify(本月的最后一天);
$ period = new \DatePeriod(
$ first_day,
\DateInterval :: createFromDateString('this week Sunday'),
$ last_day,
\ DatePeriod :: EXCLUDE_START_DATE
);

$ weeks = [$ first_day-> format($ format)];
foreach($ period as $ d){
$ weeks [] = $ d-> modify(’-1 day’)-> format($ format);
$ weeks [] = $ d-> format($ format);
}
$ weeks [] = $ last_day-> format($ format);

return array_chunk($ weeks,2);
}

echo当月周数:\n;
$ weeks = getWeekDates(new \DateTime(’now’));
array_walk($ weeks,function($ week){
vprintf(%s to%s\n,$ week);
});

echo \n 2016年5月的一周:\n;
$ weeks = getWeekDates(new \DateTime(’2016-05’));
array_walk($ weeks,function($ week){
vprintf(%s to%s\n,$ week);
});

echo \n 2016年6月的一周:\n;
$ weeks = getWeekDates(new \DateTime(’2016-06’));
array_walk($ weeks,function($ week){
vprintf(%s to%s\n,$ week);
});

echo \n 2016年10月的一周:\n;
$ weeks = getWeekDates(new \DateTime(’2016-10’));
array_walk($ weeks,function($ week){
vprintf(%s to%s\n,$ week);
});

结果:

 当月周:
2016-05-01至2016-05-07
2016-05-08至2016-05-14
2016-05-15至2016-05-21
2016-05-22至2016-05-28
2016-05-29至2016-05-31

2016年5月的星期:
2016-05-01至2016-05-07
2016-05-08至2016-05-14
2016 -05-15至2016-05-21
2016-05-22至2016-05-28
2016-05-29至2016-05-31

2016年6月:
2016-06-01至2016-06-04
2016-06-05至2016-06-11
2016-06-12至2016-06-18
2016-06-19至2016-06-25
2016-06-26至2016-06-30

2016年10月的星期:
2016 -10-01至2016-10-01
2016-10-02至2016-10-08
2016-10-09至2016-10-15
2016-10-16至2016-10-22
2016-10-23至2016-10-29
2016-10-30至2016-10-31


I want to split the current month in to weeks like first day to saturday and then sunday to next saturday.For ex: for May month, So i want to split that like

2016-05-01 to 2016-05-07
2016-05-08 to 2016-05-14
2016-05-15 to 2016-05-21
2016-05-22 to 2016-05-28
2016-05-29 to 2016-05-31

if i try the below code,i didnt get exact result.

<?php

$start_date = date('Y-m-d', strtotime('2016-06-01'));
$end_date = date('Y-m-d', strtotime('2016-06-30'));
$i=1;
for($date = $start_date; $date <= $end_date; $date = date('Y-m-d',       strtotime($date. ' + 7 days'))) {
    echo getWeekDates($date, $start_date, $end_date, $i);
    echo "\n";
    $i++;
}

function getWeekDates($date, $start_date, $end_date, $i) {
    $week =  date('W', strtotime($date));
    $year =  date('Y', strtotime($date));

    $from = date("Y-m-d", strtotime("{$year}-W{$week}+1")); 
    if($from < $start_date) $from = $start_date;

    $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   
    if($to > $end_date) $to = $end_date;

    echo $from." - ".$to.'<br>';    
}

?>

i got like

2016-05-01 - 2016-05-01
2016-05-01 - 2016-05-08
2016-05-08 - 2016-05-15
2016-05-15 - 2016-05-22
2016-05-22 - 2016-05-29

In for loop, while checking last set of days, conditon get failed .so it doesnt calculate 30,31.

How can i do that?

解决方案

DateTime, DatePeriod and DateInterval are your friends to do that quickly!

See online.

<?php
/**
 * @file
 * Awnser to http://stackoverflow.com/q/37224961/392725
 *
 * @link http://stackoverflow.com/a/37228497/392725
 */
date_default_timezone_set('Europe/Paris');

function getWeekDates(DateTimeInterface $date, $format = 'Y-m-d') {
  $dt        = \DateTimeImmutable::createFromMutable($date);
  $first_day = $dt->modify('first day of this month');
  $last_day  = $dt->modify('last day of this month');
  $period    = new \DatePeriod(
    $first_day,
    \DateInterval::createFromDateString('sunday this week'),
    $last_day,
    \DatePeriod::EXCLUDE_START_DATE
  );

  $weeks = [$first_day->format($format)];
  foreach ($period as $d) {
    $weeks[] = $d->modify('-1 day')->format($format);
    $weeks[] = $d->format($format);
  }
  $weeks[] = $last_day->format($format);

  return array_chunk($weeks, 2);
}

echo "Weeks of the current month:\n";
$weeks = getWeekDates(new \DateTime('now'));
array_walk($weeks, function ($week) {
  vprintf("%s to %s\n", $week);
});

echo "\nWeeks of the month may 2016:\n";
$weeks = getWeekDates(new \DateTime('2016-05'));
array_walk($weeks, function ($week) {
  vprintf("%s to %s\n", $week);
});

echo "\nWeeks of the month june 2016:\n";
$weeks = getWeekDates(new \DateTime('2016-06'));
array_walk($weeks, function ($week) {
  vprintf("%s to %s\n", $week);
});

echo "\nWeeks of the month october 2016:\n";
$weeks = getWeekDates(new \DateTime('2016-10'));
array_walk($weeks, function ($week) {
  vprintf("%s to %s\n", $week);
});

Result:

Weeks of the current month:
2016-05-01 to 2016-05-07
2016-05-08 to 2016-05-14
2016-05-15 to 2016-05-21
2016-05-22 to 2016-05-28
2016-05-29 to 2016-05-31

Weeks of the month may 2016:
2016-05-01 to 2016-05-07
2016-05-08 to 2016-05-14
2016-05-15 to 2016-05-21
2016-05-22 to 2016-05-28
2016-05-29 to 2016-05-31

Weeks of the month june 2016:
2016-06-01 to 2016-06-04
2016-06-05 to 2016-06-11
2016-06-12 to 2016-06-18
2016-06-19 to 2016-06-25
2016-06-26 to 2016-06-30

Weeks of the month october 2016:
2016-10-01 to 2016-10-01
2016-10-02 to 2016-10-08
2016-10-09 to 2016-10-15
2016-10-16 to 2016-10-22
2016-10-23 to 2016-10-29
2016-10-30 to 2016-10-31

这篇关于在php中将当前月份拆分为几周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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