標題有點難下
需求主要如下
我要抓當月到今天的週別及週三日期
如果當週還沒有到週三或是當週無週三則抓上週三日期
以今天9/10為例
9月行事曆簡圖如下 (到今天9/10)
一 二 三 四 五 六 日
1
2 3 4 5 6 7 8
9 10
根據上圖 我要的週別及日期是W35 8/28、W36 9/5、W37 9/5
說明如下
9月從W35開始
W35 因為不是星期三 所以抓上週三的日期 所以是8/28
W36 9/5是週三 所以抓出9/5
W37 目前只到9/10(二) 所以抓上週三日期 9/5
再請各位指教
謝謝 !
-- 題目沒有說明得很清楚 我是要以週別為基準
9月是W35開始 到目前W37 所以我只取三次資料
-- 後來想到寫法了 補充如下 方法可能有點笨 但可行
//當月第一天
$first_day = date('Y/m/01',time());
//9月起始週別到目前週別
while (date('W',strtotime($first_day)) <= date('W',strtotime(date('Y/m/d')))){
$week_date = new DateTime();
$week_date->setISODate(date('Y'),date('W',strtotime($first_day)));
$week_first_date = $week_date->format('Y/m/d');
//如果該週別的週三>今天 則取上週三的日期
if (date('Y/m/d',strtotime($week_first_date .'+2 day')) > date('Y/m/d') ){
echo "週別: ".date('W',strtotime($week_first_date));
echo "<br>";
echo "週三日期: ".date('Y/m/d',strtotime($week_first_date .'-5 day'));
echo "<br>";
//如果該週別的週三<=今天 則取該週三的日期
}else{
echo "週別: ".date('W',strtotime($week_first_date));
echo "<br>";
echo "週三日期: ".date('Y/m/d',strtotime($week_first_date .'+2 day'));
echo "<br>";
}
$first_day = date("Y/m/d",strtotime($first_day.'+7 day'));
}
$Start = date('Y-m-01'); // 起始日
$Today = date('Y-m-d'); // 當前日
/* 計算 起始日與上週三相差天數 0~6 */
$LastWed = date('N', strtotime( $Start ))-3;
if( $LastWed<0 ){ $LastWed+=7; }
/* 起始日扣除相差天數,得 實際起始日(週三) */
$StartWed = date('Y-m-d', strtotime( "$Start -$LastWed day" ));
echo "<pre>$StartWed ~ $Today</pre>";
$begin = strtotime( $StartWed );
while( $begin<=strtotime( $Today ) ){
$WeekNumber = date('W', $begin);
$Wednesday = date('Y-m-d', $begin);
echo "<pre>W$WeekNumber: $Wednesday</pre>";
$begin = strtotime( "$Wednesday +1 week" ); // 週遞增
}
標題也可以下給定日期範圍,取得不重覆的前一個星期三的日期及週別
<?php
$begin = new DateTime( '2019-09-01' );
$end = new DateTime( '2019-09-10' );
$date = $begin;
while ($date <= $end) {
if (date("w", $date->getTimestamp())==3) { //若是星期三就是本星期三
$wed = strtotime("wednesday", $date->getTimestamp());
} else { //若不是星期三就取前一個星期三
$wed = strtotime("last wednesday", $date->getTimestamp());
}
$weekOfYear = date("W", $date->getTimestamp());
echo date("Y/m/d", $date->getTimestamp())." "."W".$weekOfYear." ".date('Y/m/d',$wed) . "\n";
$date->add(new DateInterval('P1D'));
}
?>
選我正解
https://ideone.com/0j03Hd
不過因為是用iso標準,一星期第一天是星期一。
都用最基本的date功能而已,PHP應該都有相對應的。
from datetime import date, timedelta
def getLastWed(d):
if d.isoweekday() >= 3:
delta = d.isoweekday() - 3
else:
delta = d.isoweekday() + 7 - 3
return d - timedelta(days=delta)
def getLast3WedInMonth(d):
weeknum = d.isocalendar()[1]
startWeeknum = date(d.year, d.month, 1).isocalendar()[1]
if d.isoweekday() >= 3:
thisWed = getLastWed(d)
lastWed = getLastWed(thisWed - timedelta(days=1))
last2Wed = getLastWed(lastWed - timedelta(days=1))
else:
thisWed = getLastWed(d)
lastWed = thisWed
last2Wed = getLastWed(lastWed - timedelta(days=1))
return list(filter(lambda x: x["weeknumber"] >= startWeeknum,
[
{"weeknumber": weeknum-2, 'date': last2Wed},
{"weeknumber": weeknum-1, 'date': lastWed},
{"weeknumber": weeknum, 'date': thisWed}
]))
print(getLast3WedInMonth(date.today()))
print(getLast3WedInMonth(date(2019, 9, 11)))
print(getLast3WedInMonth(date(2019, 9, 10)))
print(getLast3WedInMonth(date(2019, 9, 4)))
看不懂幹嘛要定基準日...