0

今日の天気をチェックする方法は、PHPで1年の最初の日かどうかです。

編集:echo date('Ymd'、strToTime( '1/1 this year'));

このコードは私に結果を与えました。これは有効ですか、それともこれよりも良い方法がありますか。

4

9 に答える 9

6

フォーマット文字列 'z' を組み込みのdate()関数に渡すことで、年間通算日を取得できます。これは、最初の日には '0'、2 番目には '1' を返します... (うるう) 年の最後の日は '365' までです。

if ( date('z') === '0' ) {
    //Today is the first day of the year.
}
于 2012-08-17T07:27:49.453 に答える
2
if (date('z') === 0) {
echo 'we have 1.1.'
}

http://php.net/manual/en/function.date.php

于 2012-08-17T07:28:25.917 に答える
0

次のことができます:

echo date('Y-m-d', strtotime("first day of january " . date('Y')));
于 2012-08-17T08:04:21.747 に答える
0

元旦の名前が気になる方は

echo date('l', strtotime('2012-01-01'));

また

echo date('l', strtotime("first day of January 2012"));

Output:// Sunday

OR

just use -

echo (date('z') === 0) ? 'First day' : 'Not first day';

//z     The day of the year (starting from 0)   0 through 365

http://php.net/manual/en/function.date.php

http://www.php.net/manual/en/datetime.formats.relative.php

于 2012-08-17T07:43:20.293 に答える
0

詳細については、http://php.net/dateを参照してください。

<?php
 if(date('dm',mktime())=="0101")
    echo "fist day of year";
 else
    echo "not the first day";
?>
于 2012-08-17T07:29:49.937 に答える
0

年の最初の日は 1/1/yyyy と変わらない strcmp または

$now=new DateTime();

次に、getDay GetMonth from $now を使用して評価します

于 2012-08-17T07:31:16.473 に答える
0

This should solve your problem and more!

<?php
$theDate = date("md"); 
if ($theDate=="0101") echo "It's A New Year!";
?>

Date() function formatting characters:

  • d - A numeric representation of the day of the month (0 - 31)
  • m - A numeric representation of the month of the year (1 - 12)
  • y - A numeric representation (two digit) of a year (99, 00, 01, 02)
  • H - 24 hour format current hour (00 - 23)
  • i - Minutes (00 - 59)
  • s - Seconds (00 - 59)
于 2012-08-17T07:25:26.753 に答える
0
if(date('j') == '1' && date('n') == 1){
    // if first day of year
}

これにより、通常は心配しなければならない複雑な日付を使用することなく、簡単に実行できます。

于 2012-08-17T07:27:04.847 に答える
0

date関数にはzフォーマット文字があり、これを使用できます。

$day_of_year = date('z');
// $day_of_year is 259 for the day I write this
于 2015-09-17T09:45:50.790 に答える