PHP计算一年有多少周,每周开始日期和结束日期
2016-04-11 12:29
615 查看
一年有多个周,每周的开始日期和结束日期
参考代码一:[正在使用的版本]
参考代码二:[还没去验证]
参考代码一:[正在使用的版本]
<?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]; ?>
相关文章推荐
- PHP安装、配置及拓展总结
- 增加phpmyadmin导入文件上限
- PHP实现的通过参数生成MYSQL语句类完整实例
- PHP-关于模板的原理和解析
- PHP实现的浏览器检查类
- PHP中Exception异常
- PHP学习笔记
- PHP模板引擎Smarty内建函数section,sectionelse用法详解
- PHP格式化字符串函数 sprintf()
- PHP实现多服务器共享SESSION数据-mysql实现
- 100个常用的 PHP 类库、资源和技巧小结
- ands TFTP Ping ICMP FTP
- PHP模板引擎Smarty内建函数详解
- php 魔术方法 说明
- 各种有用的PHP开源库收集
- php各个模式、版本的区别
- PHP模板引擎Smarty内置变量调解器用法详解
- PHP多维数组排序
- Session [php]
- PHP PDO ACCESS 留言板-php转换成HTML页面文件maketohtml.php