php時間の処理について
8460 ワード
1.先月の初日と最終日を取得します. echo date('Y-m-01', strtotime('-1 month')); echo ""; echo date('Y-m-t', strtotime('-1 month')); echo "";
上の方法はいくつか問題があって、bg 5 hfcの返事によって下へ修正します
先月初日:echo date('Y-m-d',strtotime('Y-m-01').'-1 month'));//今月の初日からあと1か月減額する
先月最終日:echo date('Y-m-d',strtotime('Y-m-01').'-1 day'));//今月の1日目をさらに1日減らすと計算する
2.当月の初日と最終日を取得する. $BeginDate=date('Y-m-01', strtotime(date("Y-m-d"))); echo $BeginDate; echo ""; echo date('Y-m-d', strtotime("$BeginDate +1 month -1 day")); echo "";
3.当日の年、月、日及び日数を取得する.echo"今月共有:".date("t").「天」echo「現在の年」.date('Y');echo「現在の月」.date('m');echo「現在の番号」.date('d'); echo "";
4.関数および配列を使用して、その月の初日と最終日を取得し、実用的なfunction getthemonth($date){$firstday=date('Y-m-01',strtotime($date);$lastday=date('Y-m-d',strtotime('$firstday+1 month-1 day);return array($firstday,$lastday); } $today = date("Y-m-d"); $day=getthemonth($today);echo「当月の初日:」.$day[0].「当月の最終日:」.$day[1]; echo "";
今月の日付を取得:
$firstdayは月の初日ですが、$dateが2018-2であれば$firstdayは2018-02-01、そして$firstdayによってプラス1ヶ月で2018-03-01、さらにマイナス1日で2018-02-28となり、date()とstrtotime()で便利です.
先月の日付を取得:
先月の日付はまずタイムスタンプを取得して、それから月に-1すればOKで、超スマートなdate()は2018-0-1というものを2017-12-01に変換して、とても爽やかです.
次の日付を取得します.
来月の日付のコードは少し長いように見えますが、date()は2018-13-01のようなものが回らないので、1970に戻るので、12月の問題を前に処理する必要があります.12月を除いては直接月+1でOKです.
どちらかというと、やはり便利で、日付関数が強すぎます.
上の方法はいくつか問題があって、bg 5 hfcの返事によって下へ修正します
先月初日:echo date('Y-m-d',strtotime('Y-m-01').'-1 month'));//今月の初日からあと1か月減額する
先月最終日:echo date('Y-m-d',strtotime('Y-m-01').'-1 day'));//今月の1日目をさらに1日減らすと計算する
2.当月の初日と最終日を取得する. $BeginDate=date('Y-m-01', strtotime(date("Y-m-d"))); echo $BeginDate; echo ""; echo date('Y-m-d', strtotime("$BeginDate +1 month -1 day")); echo "";
3.当日の年、月、日及び日数を取得する.echo"今月共有:".date("t").「天」echo「現在の年」.date('Y');echo「現在の月」.date('m');echo「現在の番号」.date('d'); echo "";
4.関数および配列を使用して、その月の初日と最終日を取得し、実用的なfunction getthemonth($date){$firstday=date('Y-m-01',strtotime($date);$lastday=date('Y-m-d',strtotime('$firstday+1 month-1 day);return array($firstday,$lastday); } $today = date("Y-m-d"); $day=getthemonth($today);echo「当月の初日:」.$day[0].「当月の最終日:」.$day[1]; echo "";
今月の日付を取得:
function
getMonth(
$date
){
$firstday
=
date
(
"Y-m-01"
,
strtotime
(
$date
));
$lastday
=
date
(
"Y-m-d"
,
strtotime
(
"$firstday +1 month -1 day"
));
return
array
(
$firstday
,
$lastday
);
}
$firstdayは月の初日ですが、$dateが2018-2であれば$firstdayは2018-02-01、そして$firstdayによってプラス1ヶ月で2018-03-01、さらにマイナス1日で2018-02-28となり、date()とstrtotime()で便利です.
先月の日付を取得:
function
getlastMonthDays(
$date
){
$timestamp
=
strtotime
(
$date
);
$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"
));
return
array
(
$firstday
,
$lastday
);
}
先月の日付はまずタイムスタンプを取得して、それから月に-1すればOKで、超スマートなdate()は2018-0-1というものを2017-12-01に変換して、とても爽やかです.
次の日付を取得します.
function
getNextMonthDays(
$date
){
$timestamp
=
strtotime
(
$date
);
$arr
=
getdate
(
$timestamp
);
if
(
$arr
[
'mon'
] == 12){
$year
=
$arr
[
'year'
] +1;
$month
=
$arr
[
'mon'
] -11;
$firstday
=
$year
.
'-0'
.
$month
.
'-01'
;
$lastday
=
date
(
'Y-m-d'
,
strtotime
(
"$firstday +1 month -1 day"
));
}
else
{
$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"
));
}
return
array
(
$firstday
,
$lastday
);
}
来月の日付のコードは少し長いように見えますが、date()は2018-13-01のようなものが回らないので、1970に戻るので、12月の問題を前に処理する必要があります.12月を除いては直接月+1でOKです.
どちらかというと、やはり便利で、日付関数が強すぎます.