生活随笔
收集整理的這篇文章主要介紹了
用strtotime()和date()函数算出2019年9月的周日日期
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
strtotime---用于接收兩個參數,第一個參數是格式化的日期數據如:date('Y-m-d'),第二個參數有如'+7 day'
<?
php
$firstsunday = strtotime(date(
'Y-m-01'));
$thisy = intval(date(
'm'));
$diffdays=
0;
$count =
0;
if (date(
'w',strtotime(date(
'Y-m-01')))!=
0)
{$diffdays=
7-date(
'w',strtotime(date(
'Y-m-01')));
}
else{$count++
;
}
$startdate =strtotime(date(
'Y-m-01').
"+".$diffdays.
"day");
while (True)
{if ($thisy!=intval(date(
'm',$startdate))){break;}echo date('Y-m-d',$startdate).
"\n";$startdate = strtotime(date(
'Y-m-d',strtotime(date(
'Y-m-d',$startdate).
"+7 day")));//
}
結果
2019-
09-
01
2019-
09-
08
2019-
09-
15
2019-
09-
22
2019-
09-
29 - ?類版本---date()的參數可以是‘2019-09-20’這樣的字符串,如同date('2019-09-20')或者是strtotime返回的整型數如同date('Y-m-d',strtotime('2019-09-20))
class mycalendar
{function __construct($year,$mon){$this->nianyue=$year.
'-'.$mon.
'-'.
'01';$this->firstday=strtotime(date($
this->
nianyue));$this->m = intval(date(
'm',$
this->
firstday));$this->wdaylist=
array();echo "時間戳:".date(
'Y-m-d',$
this->firstday).
"\n";}function startday(){$w = date(
'w',$
this->
firstday);$diffdays=
0;if ($w!=
0){$diffdays=
7-
$w;}$this->firstday = strtotime(date(
'Y-m-d',strtotime($
this->nianyue)).
"+".$diffdays.
" day");//array_push($this->wdaylist,$w);//echo "\n".date('Y-m-d',$this->firstday);//echo "\n".$this->firstday;return $
this->
firstday;}function addwday(){$curday = $
this->
startday();while (
true){if(intval(date(
'm',$curday)!=$
this->
m)){break;}echo date('Y-m-d',$curday).
"\n";array_push($this->wdaylist,date(
'Y-m-d',$curday));$curday=strtotime(date(
'Y-m-d',$curday).
"+7 day");}return $
this->
wdaylist;}
}
$mycal =
new mycalendar(
2019,
10);
var_dump($mycal->addwday());
?
結果:
時間戳:2019-10-01
2019-10-06
2019-10-13
2019-10-20
2019-10-27
array(4) {
? [0]=>
? string(10) "2019-10-06"
? [1]=>
? string(10) "2019-10-13"
? [2]=>
? string(10) "2019-10-20"
? [3]=>
? string(10) "2019-10-27"
}
echo date(
'Y-m-d',strtotime(date(
'2019-10-01').
"+1 month -1 day"));
輸出結果:
2019-
10-
31 - ?注意!對于日期計算一定要,正確的使用參數,否則將會出錯
$lastday = strtotime(date(
'Y-m-d',$
this->time).
"+1 day");#正確
$lastday = strtotime(date($this->time)."+1 day");#錯誤,這樣會返回1970-01-01 ?
轉載于:https://www.cnblogs.com/saintdingspage/p/11561246.html
總結
以上是生活随笔為你收集整理的用strtotime()和date()函数算出2019年9月的周日日期的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。