php年月日に基づいて当月日数および日付配列を取得する方法
4431 ワード
この例では,phpが年月に基づいて当月日数および日付配列を取得する方法について述べる.皆さんの参考にしてください.具体的には以下の通りです.
実行結果:31
改造、日付配列を返す:
実行結果は次のとおりです.
PHPに関する詳細について興味のある読者は、「php日付と時間用法総括」、「PHP配列(Array)操作技巧大全」、「PHP基本文法入門教程」、「PHP演算と演算子用法総括」、「php対象プロセス設計入門教程」、「PHPネットワークプログラミング技巧総括」、「php文字列(string)用法総括」を参照してください.、「php+mysqlデータベース操作入門チュートリアル」および「php一般データベース操作テクニック要約」
ここで述べたことが皆さんのPHPプログラム設計に役立つことを願っています.
function get_day( $date )
{
$tem = explode('-' , $date); //
$year = $tem['0'];
$month = $tem['1'];
if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12)))
{
// $text = $year.' '.$month.' 31 ';
$text = '31';
}
elseif( $month == 2 )
{
if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //
{
// $text = $year.' '.$month.' 29 ';
$text = '29';
}
else{
// $text = $year.' '.$month.' 28 ';
$text = '28';
}
}
else{
// $text = $year.' '.$month.' 30 ';
$text = '30';
}
return $text;
}
echo get_day('2016-8-1');
実行結果:31
改造、日付配列を返す:
/**
*
* @param $date
* @param $rtype 1 2
* @return
*/
function get_day( $date ,$rtype = '1')
{
$tem = explode('-' , $date); //
$year = $tem['0'];
$month = $tem['1'];
if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12)))
{
// $text = $year.' '.$month.' 31 ';
$text = '31';
}
elseif( $month == 2 )
{
if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //
{
// $text = $year.' '.$month.' 29 ';
$text = '29';
}
else{
// $text = $year.' '.$month.' 28 ';
$text = '28';
}
}
else{
// $text = $year.' '.$month.' 30 ';
$text = '30';
}
if ($rtype == '2') {
for ($i = 1; $i <= $text ; $i ++ ) {
$r[] = $year."-".$month."-".$i;
}
} else {
$r = $text;
}
return $r;
}
var_dump(get_day('2016-8-1','2'));
実行結果は次のとおりです.
array(31) {
[0]=>
string(8) "2016-8-1"
[1]=>
string(8) "2016-8-2"
[2]=>
string(8) "2016-8-3"
[3]=>
string(8) "2016-8-4"
[4]=>
string(8) "2016-8-5"
[5]=>
string(8) "2016-8-6"
[6]=>
string(8) "2016-8-7"
[7]=>
string(8) "2016-8-8"
[8]=>
string(8) "2016-8-9"
[9]=>
string(9) "2016-8-10"
[10]=>
string(9) "2016-8-11"
[11]=>
string(9) "2016-8-12"
[12]=>
string(9) "2016-8-13"
[13]=>
string(9) "2016-8-14"
[14]=>
string(9) "2016-8-15"
[15]=>
string(9) "2016-8-16"
[16]=>
string(9) "2016-8-17"
[17]=>
string(9) "2016-8-18"
[18]=>
string(9) "2016-8-19"
[19]=>
string(9) "2016-8-20"
[20]=>
string(9) "2016-8-21"
[21]=>
string(9) "2016-8-22"
[22]=>
string(9) "2016-8-23"
[23]=>
string(9) "2016-8-24"
[24]=>
string(9) "2016-8-25"
[25]=>
string(9) "2016-8-26"
[26]=>
string(9) "2016-8-27"
[27]=>
string(9) "2016-8-28"
[28]=>
string(9) "2016-8-29"
[29]=>
string(9) "2016-8-30"
[30]=>
string(9) "2016-8-31"
}
PHPに関する詳細について興味のある読者は、「php日付と時間用法総括」、「PHP配列(Array)操作技巧大全」、「PHP基本文法入門教程」、「PHP演算と演算子用法総括」、「php対象プロセス設計入門教程」、「PHPネットワークプログラミング技巧総括」、「php文字列(string)用法総括」を参照してください.、「php+mysqlデータベース操作入門チュートリアル」および「php一般データベース操作テクニック要約」
ここで述べたことが皆さんのPHPプログラム設計に役立つことを願っています.