[英]How to get first, third and fifth Saturday of a month in PHP?
我已經找到了解決這個問題的方法,但是在某些情況下它出錯了。 以下是我的發現:
此代碼段將打印2015年8月的第一個星期六和2015年7月:
$sat = date("F Y",strtotime("2015-08-01"));
echo $fir_sat = "$sat first saturday";
echo " is ".date('d', strtotime($fir_sat));
$sat = date("F Y",strtotime("2015-07-04"));
echo $fir_sat = "$sat first saturday";
echo " is ".date('d', strtotime($fir_sat));
以下是輸出:
August 2015 first Saturday is 08
July 2015 first Saturday is 04
但實際上它是01 :
August 2015 first Saturday is 01
怎么會發生? 解決辦法是什么?
根據您的反饋,我嘗試了一些編碼。 以下是我的發現。 錯誤發生是因為php版本。
在像5.2.9這樣的較低版本的PHP中,以下代碼不起作用
回音日期('Ym-d',strtotime('2015年8月的第一個星期六'));
但是在像5.4這樣的更高版本中,它正在萎縮
有任何想法嗎?
嘗試使用of
作為
echo date('d F Y', strtotime('first saturday of $fir_sat'));
“ordinal dayname”的''不會提前到另一天。
代替
echo date('d F Y', strtotime('$fir_sat first saturday'));
“序數日”確實提前到了另一天。
您還可以查看文檔(備注)
這樣你就可以利用strtotime功能。
echo date('d F Y', strtotime('first saturday of August 2015'));
echo "<br>";
echo date('d F Y', strtotime('second saturday of August 2015'));
echo "<br>";
echo date('d F Y', strtotime('fifth saturday of August 2015'));
確保你最后通過適當的年份和月份,因為它會給你正確的日期。
謝謝
阿米特
試試這個
echo date('Y-m-d', strtotime('first saturday of august 2015'));
echo date('Y-m-d', strtotime('third saturday of august 2015'));
echo date('Y-m-d', strtotime('fifth saturday of august 2015'));
簡單的解決方案,在我當地測試。
$sat = date("d F Y",strtotime("2015-08-01"));
echo $fir_sat = "$sat<br> First saturday";
echo " is ".date('d', ($fir_sat));
$thi_sat = strtotime ( '+14 day' , strtotime ( $sat ) ) ;
$thi_sat = date ( 'd' , $thi_sat );
echo "<br>third Saturday at ".$thi_sat;
$fifth_sat = strtotime ( '+28 day' , strtotime ( $sat ) ) ;
$fifth_sat = date ( 'd' , $fifth_sat );
echo "<br>Fifth Saturday at ".$fifth_sat;
輸出是:
01 August 2015
First saturday is 01
third Saturday at 15
Fifth Saturday at 29
這可能會有所幫助。歡呼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.