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获取一年有几周以及每周开始日期和结束日期,有所帮助。

(0)

相关推荐

  • PHP获取当前日期及本周一是几月几号的方法

    本文实例讲述了PHP获取当前日期及本周一是几月几号的方法.分享给大家供大家参考,具体如下: <?php header("content-type:text/html;charset=utf-8"); date_default_timezone_set('PRC'); function getWeek($unixTime=''){ $unixTime=is_numeric($unixTime)?$unixTime:time(); $weekarray=array('日','一','

  • php获取本周开始日期和结束日期的方法

    本文实例讲述了php获取本周开始日期和结束日期的方法.分享给大家供大家参考.具体如下: 复制代码 代码如下: //当前日期  $sdefaultDate = date("Y-m-d");  //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期  $first=1;  //获取当前周的第几天 周日是 0 周一到周六是 1 - 6  $w=date('w',strtotime($sdefaultDate));  //获取本周开始日期,如果$w是0,则表示周日,减去 6

  • php计算给定日期所在周的开始日期和结束日期示例

    本文实例讲述了php计算给定日期所在周的开始日期和结束日期.分享给大家供大家参考,具体如下: <?php /** * 取得给定日期所在周的开始日期和结束日期 * @param string $gdate 日期,默认为当天,格式:YYYY-MM-DD * @param int $weekStart 一周以星期一还是星期天开始,0为星期天,1为星期一 * @return array 数组array( "开始日期 ", "结束日期"); */ function ge

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

    项目中需要做个提交周报的功能,需要知道指定周数的开始日期和结束日期,以便处理其他业务.以下是一段通过PHP来获取一年中的每星期的开始日期和结束日期的代码,与大家分享. 复制代码 代码如下: function get_week($year) {     $year_start = $year . "-01-01";     $year_end = $year . "-12-31";     $startday = strtotime($year_start);    

  • 用php获取本周,上周,本月,上月,本季度日期的代码

    复制代码 代码如下: echo date("Ymd",strtotime("now")), "\n"; echo date("Ymd",strtotime("-1 week Monday")), "\n"; echo date("Ymd",strtotime("-1 week Sunday")), "\n"; echo date

  • PHP计算指定日期所在周的开始和结束日期的方法

    本文实例讲述了PHP计算指定日期所在周的开始和结束日期的方法.分享给大家供大家参考.具体实现方法如下: <html> <head> <title>计算一周开始结束日期</title> </head> <body> <form method="post" action="./index.html" enctype="utf-8"> <table> <

  • php获取本周星期一具体日期的方法

    本文实例讲述了php获取本周星期一具体日期的方法.分享给大家供大家参考.具体如下: private function mondayTime($timestamp=0,$is_return_timestamp=true){ static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $monday_date = date('Y

  • 计算一段日期内的周末天数的php代码(星期六,星期日总和)

    复制代码 代码如下: /* | Author: Yang Yu <niceses@gmail.com> | @param char|int $start_date 一个有效的日期格式,例如:20091016,2009-10-16 | @param char|int $end_date 同上 | @return 给定日期之间的周末天数 */ function get_weekend_days($start_date,$end_date){ if (strtotime($start_date) &

  • PHP实现针对日期,月数,天数,周数,小时,分,秒等的加减运算示例【基于strtotime】

    本文实例讲述了PHP实现针对日期,月数,天数,周数,小时,分,秒等的加减运算方法.分享给大家供大家参考,具体如下: 其实就是strtotime这个内置函数 //PHP 日期 加减 周 date("Y-m-d",strtotime("2013-11-12 +1 week")) //PHP 日期 加减 天数 date("Y-m-d",strtotime("2013-11-12 12:12:12 +1 day")) //PHP 日期

  • PHP判断两个给定日期是否在同一周的方法

    本文实例讲述了PHP判断两个给定日期是否在同一周的方法.分享给大家供大家参考,具体如下: /** * 判断两日期是不是同一周 * 星期是按周日到周六 */ function getSameWeek($pretime,$aftertime){ $flag = false;//默认不是同一周 $afweek = date('w',$aftertime);//当前是星期几 $mintime = $aftertime - $afweek * 3600*24;//一周开始时间 $maxtime = $af

随机推荐