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

5年以前  |  阅读数:1037 次  |  编程语言:PHP 

最近接了一个项目,其中有一需求是用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];
    ?>

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

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例