Home  >  Article  >  Backend Development  >  PHP gets the number of weeks in a year, the start date and end date of each week, and the start date of weeks_PHP Tutorial

PHP gets the number of weeks in a year, the start date and end date of each week, and the start date of weeks_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:45:00910browse

PHP gets the number of weeks in a year, the start date and end date of each week, and the start date of weeks

I recently took on a project, and one of the requirements is to use php to get the year There are weeks as well as weekly start and contact dates. I couldn’t find any suitable information on the Internet, so I made one myself. Here are two ways to implement PHP to get the number of weeks in a year and the start and end dates of each week

Code 1:

<&#63;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']);
&#63;>

Code 2:

<&#63;php
header("Content-type:text/html;charset=utf-8");
function getIsoWeeksInYear($year)
{
 $date = new DateTime;
 $date->setISODate($year, 53);
 return ($date->format("W") === "53" &#63; 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];
&#63;>

The entire content of the above article, I hope it will be helpful for everyone to learn PHP to get the number of weeks in a year and the start and end dates of each week.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1044256.htmlTechArticlePHP gets the number of weeks in a year and the start and end dates of each week, and the start date of the week is the most recent one Project, one of the requirements is to use php to get the weeks of the year and the start date of each week...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn