本文目錄一覽:
- 1、php中怎麼輸出2015年2月份有多少天
- 2、PHP計算一年多少個星期和每周的開始和結束日期
- 3、PHP 根據年、周數獲取周的起止日期
- 4、PHP 計算某日是這一年的第幾周
- 5、php 根據多少天算幾周 比如15天 2周+1天 這如何算?
php中怎麼輸出2015年2月份有多少天
用這個函數:int cal_days_in_month ( int $calendar , int $month , int $year )
例子:
?php
$number = cal_days_in_month(CAL_GREGORIAN, 2, 2015); // 28
echo “There were {$number} days in August 2015”;
?
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 2013br’ . getIsoWeeksInYear(2013);
$weekday = weekday(strtotime(date(‘d-m-Y’, strtotime(‘5-8-2013’))));
echo ‘br 10-8-2013’;
echo ‘brStart: ‘ . $weekday[0];
echo ‘brEnd: ‘ . $weekday[1];
?
或者方法三:
function get_week($year) {
$year_start = $year . “-01-01”;
$year_end = $year . “-12-31”;
$startday = strtotime($year_start);
if (intval(date(‘N’, $startday)) != ‘1’) {
$startday = strtotime(“next monday”, strtotime($year_start)); //獲取年第一周的日期
}
$year_mondy = date(“Y-m-d”, $startday); //獲取年第一周的日期
$endday = strtotime($year_end);
if (intval(date(‘W’, $endday)) == ‘7’) {
$endday = strtotime(“last sunday”, strtotime($year_end));
}
$num = intval(date(‘W’, $endday));
for ($i = 1; $i = $num; $i++) {
$j = $i -1;
$start_date = date(“Y-m-d”, strtotime(“$year_mondy $j week “));
$end_day = date(“Y-m-d”, strtotime(“$start_date +6 day”));
$week_array[$i] = array (
str_replace(“-“,
“.”,
$start_date
), str_replace(“-“, “.”, $end_day));
}
return $week_array;
}
函數get_week()通過傳入參數$year年份,獲取當年第一天和最後一天所在的周數,計算第一周的日期,通過循環獲取每一周的第一天和最後一天的日期。最後返回是一個數組。
想得到指定周數的開始日期和結束日期,比如2011年第18周的開始日期和結束日期,代碼如下:
複製代碼 代碼如下:
$weeks = get_week(2011);
echo ‘第18周開始日期:’.$weeks[18][0].”;
echo ‘第18周結束日期:’.$weeks[18][1];
最後輸出結果:
第18周開始日期:2011.05.02
第18周結束日期:2011.05.08
PHP 根據年、周數獲取周的起止日期
我也是來找答案的,結果發現兩位仁兄都差了點。
那就讓我補上這最後這點吧
function getFirstDayOfWeek($year,$week)
{
$first_day = strtotime($year.”-01-01″);
$is_monday = date(“w”, $first_day) == 1;
$week_one_start = !$is_monday ? strtotime(“last monday”, $first_day) : $first_day;
return $year.’第’.$week.’周開始天:’.date(‘Y-m-d’,$week_one_start+(3600*24*7*($week-1)))
.’;結束天為:’.date(‘Y-m-d’,$week_one_start+((3600*24)*(7*($week-1)+6)));
}
PHP 計算某日是這一年的第幾周
在判斷某一天是哪一年的第幾周的時候,根據採用的國際標準(忘了叫什麼名字了),年首或者年末的那幾天有可能不屬於今年的第一周或者最後一周。
代碼如下:
?php
echo date(“oW”,strtotime(“20141229″)).”\n”;
echo date(“oW”,strtotime(‘20160101’)).”\n”;
?
擴展資料
php計算時間段的天數:
$firstday = date(“Y-m-d H:i:s”,time());//當前日期
$timestamp=strtotime($firstday);//當前日期時間戳
$firstday=date(‘Y-m-01’,strtotime(date(‘Y’,$timestamp).’-‘.(date(‘m’,$timestamp)-1).’-01′));//上個月開始的日期
$lastday=date(‘Y-m-d’,strtotime(“$firstday +1 month -1 day”));//上個月結束的日期
$stimestamp = strtotime($firstday);
$etimestamp = strtotime($lastday);// 計算日期段內有多少天
$days = ($etimestamp-$stimestamp)/86400+1;// 保存每天日期
$date = array();
for($i=0; $i$days; $i++){
$date[] = date(‘Y-m-d’, $stimestamp+(86400*$i));
}
php 根據多少天算幾周 比如15天 2周+1天 這如何算?
$allday = 15;
$weekday = 7;
$hasweek = floor($allday/$weekday);//下取整計算有幾個星期
$lessday = $allday%$weekday;//取余計算不足一星期的天數
原創文章,作者:KFRZ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/148759.html