利用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]; ?>
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。