程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> PHP獲取一年有幾周以及每周開始日期和結束日期

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獲取一年有幾周以及每周開始日期和結束日期,有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved