欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-30 09:08:31
最近接了一个项目,其中有一需求是用php获取一年有几周以及每周开始日期和接触日期。在网上找些资料没有合适的,于是自己做了一份,下面通过两种方式实现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获取一年有几周以及每周开始日期和结束日期,有所帮助。