PHP计算一年多少个星期和每周的开始和结束日期

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP计算一年多少个星期和每周的开始和结束日期相关的知识,希望对你有一定的参考价值。

php计算一年多少个星期和每周的开始和结束日期?方法如下:
方法一:
<php
header("Content-type:text/html;charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
$year = (int)$_GET['year'];
$week = (int)$_GET['week'];
$weeks = date("W", mktime(0, 0, 0, 12, 28, $year));
echo $year . '年一共有' . $weeks . '周<br />';
if ($week > $weeks || $week <= 0)

$week = 1;

if ($week < 10)

$week = '0' . $week;

$timestamp['start'] = strtotime($year . 'W' . $week);
$timestamp['end'] = strtotime('+1 week -1 day', $timestamp['start']);
echo $year . '年第' . $week . '周开始时间戳:' . $timestamp['start'] . '<br />';
echo $year . '年第' . $week . '周结束时间戳:' . $timestamp['end'] . '<br />';
echo $year . '年第' . $week . '周开始日期:' . date("Y-m-d", $timestamp['start']) . '<br />';
echo $year . '年第' . $week . '周结束日期:' . date("Y-m-d", $timestamp['end']);
?>
方法二: <?php
header("Content-type:text/html;charset=utf-8");
function getIsoWeeksInYear($year)

$date = new DateTime;
$date->setISODate($year, 53);
return ($date->format("W") === "53" ? 53 : 52);

function weekday($custom_date)

$week_start = date('d-m-Y', strtotime('this week monday', $custom_date));
$week_end = date('d-m-Y', strtotime('this week sunday', $custom_date));
$week_array[0] = $week_start;
$week_array[1] = $week_end;
return $week_array;

echo '<br> Weeks in 2013<br>' . getIsoWeeksInYear(2013);
$weekday = weekday(strtotime(date('d-m-Y', strtotime('5-8-2013'))));
echo '<br> 10-8-2013';
echo '<br>Start: ' . $weekday[0];
echo '<br>End: ' . $weekday[1];
?>
或者方法三:
function get_week($year)
$year_start = $year . "-01-01";
$year_end = $year . "-12-31";
$startday = strtotime($year_start);
if (intval(date('N', $startday)) != '1')
$startday = strtotime("next monday", strtotime($year_start)); //获取年第一周的日期

$year_mondy = date("Y-m-d", $startday); //获取年第一周的日期

$endday = strtotime($year_end);
if (intval(date('W', $endday)) == '7')
$endday = strtotime("last sunday", strtotime($year_end));


$num = intval(date('W', $endday));
for ($i = 1; $i <= $num; $i++)
$j = $i -1;
$start_date = date("Y-m-d", strtotime("$year_mondy $j week "));

$end_day = date("Y-m-d", strtotime("$start_date +6 day"));

$week_array[$i] = array (
str_replace("-",
".",
$start_date
), str_replace("-", ".", $end_day));

return $week_array;


函数get_week()通过传入参数$year年份,获取当年第一天和最后一天所在的周数,计算第一周的日期,通过循环获取每一周的第一天和最后一天的日期。最后返回是一个数组。
想得到指定周数的开始日期和结束日期,比如2011年第18周的开始日期和结束日期,代码如下:

复制代码 代码如下:

$weeks = get_week(2011);
echo '第18周开始日期:'.$weeks[18][0].'';
echo '第18周结束日期:'.$weeks[18][1];

最后输出结果:

第18周开始日期:2011.05.02
第18周结束日期:2011.05.08
参考技术A     <?php 
    function weeks_per_year($year) 
        $start = strtotime("$year-1-1 0:00:00");
        $end = strtotime("$year-12-31 24:00:00");
        $days = ($end-$start)/60/60/24;
        $w_start = date('w', $start);
        if($w_start == 0) $w_start = 7;
        $weeks = ceil(($w_start - 1 + $days) / 7.0);
        return $weeks;
    

    function week_start_end($year) 
        $start = strtotime("$year-1-1 0:00:00");
        $end = strtotime("$year-12-31 24:00:00");
        $w_start = date('w', $start);
        if($w_start == 0) $w_start = 7;
        $curr = $start;
        $curr += (7-$w_start)*24*60*60;
        $weeks = array();
        while($curr <= $end) 
            $week = array();
            $curr += 1*24*60*60;
            //week start
            $week['start'] = date('n-j', $curr);
            $curr += 6*24*60*60;
            //week end
            $week['end'] = date('n-j', $curr);
            $weeks[] = $week;
        
        return $weeks;
    

    echo weeks_per_year(2012);
    $weeks = week_start_end(2015);
    var_dump($weeks);

 ?>

本回答被提问者和网友采纳

PHP获取一年有几周以及每周开始日期和结束日期

最近接了一个项目,其中有一需求是用php获取一年有几周以及每周开始日期和接触日期。在网上找些资料没有合适的,于是自己做了一份,下面通过两种方式实现PHP获取一年有几周以及每周开始日期和结束日期

代码一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
header("Content-type:text/html;charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
$year = (int)$_GET[‘year‘];
$week = (int)$_GET[‘week‘];
$weeks = date("W", mktime(0, 0, 0, 12, 28, $year));
echo $year . ‘年一共有‘ . $weeks . ‘周<br />‘;
if ($week > $weeks || $week <= 0)
{
 $week = 1;
}
if ($week < 10)
{
 $week = ‘0‘ . $week;
}
$timestamp[‘start‘] = strtotime($year . ‘W‘ . $week);
$timestamp[‘end‘] = strtotime(‘+1 week -1 day‘, $timestamp[‘start‘]);
echo $year . ‘年第‘ . $week . ‘周开始时间戳:‘ . $timestamp[‘start‘] . ‘<br />‘;
echo $year . ‘年第‘ . $week . ‘周结束时间戳:‘ . $timestamp[‘end‘] . ‘<br />‘;
echo $year . ‘年第‘ . $week . ‘周开始日期:‘ . date("Y-m-d", $timestamp[‘start‘]) . ‘<br />‘;
echo $year . ‘年第‘ . $week . ‘周结束日期:‘ . date("Y-m-d", $timestamp[‘end‘]);
?>

代码二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
header("Content-type:text/html;charset=utf-8");
function getIsoWeeksInYear($year)
{
 $date = new DateTime;
 $date->setISODate($year, 53);
 return ($date->format("W") === "53" ? 53 : 52);
}
function weekday($custom_date)
{
 $week_start = date(‘d-m-Y‘, strtotime(‘this week monday‘, $custom_date));
 $week_end = date(‘d-m-Y‘, strtotime(‘this week sunday‘, $custom_date));
 $week_array[0] = $week_start;
 $week_array[1] = $week_end;
 return $week_array;
}
echo ‘<br> Weeks in 2013<br>‘ . getIsoWeeksInYear(2013);
$weekday = weekday(strtotime(date(‘d-m-Y‘, strtotime(‘5-8-2013‘))));
echo ‘<br> 10-8-2013‘;
echo ‘<br>Start: ‘ . $weekday[0];
echo ‘<br>End: ‘ . $weekday[1];
?>

以上本文的全部内容,希望对大家学习PHP获取一年有几周以及每周开始日期和结束日期,有所帮助。

以上是关于PHP计算一年多少个星期和每周的开始和结束日期的主要内容,如果未能解决你的问题,请参考以下文章

PHP获取一年有几周以及每周开始日期和结束日期

计算每一周的开始日期和结束日期

PHP获取一年有几周以及每周开始日期和结束日期

如何使用事件来计算 eas 中重复事件的结束日期

计算两个特定日期之间有多少个星期五(或任何一个)?

给定日期范围(开始日期和结束日期),我如何计算天数,不包括 .Net 中指定的星期几?