zoukankan      html  css  js  c++  java
  • 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; // 注意:一定要转为 2位数,否则计算出错
    }
    $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];
    ?> 
    
  • 相关阅读:
    Git checkout on a remote branch does not work
    制作ubuntu U盘安装盘
    Angular 2.0 和 1.x比较
    图文浅谈css3
    前后端数据交互方法
    CSS实现响应式全屏背景图
    html5图片高度自适应解决方法
    2014年最后100天,想说点啥。
    js常用代码
    html5+css3开发总结
  • 原文地址:https://www.cnblogs.com/52php/p/5677973.html
Copyright © 2011-2022 走看看