如何在php中查看一年中的第一天

时间:2012-08-17 07:20:32

标签: php date

如何查看今天的天气是一年中的第一天或不是在PHP。

编辑: 回音日期('Y-m-d',strToTime('今年'1/1'));

这段代码给了我结果。这是有效还是有更好的方式。

9 个答案:

答案 0 :(得分:6)

您可以通过传递格式字符串'z'来执行内置date()函数来获取一年中的某一天。这将在第一天返回'0',第二天返回'1',在(闰)年的最后一天返回'365'。

if ( date('z') === '0' ) {
    //Today is the first day of the year.
}

答案 1 :(得分:2)

if (date('z') === 0) {
echo 'we have 1.1.'
}

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

答案 2 :(得分:0)

This should solve your problem and more!

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

Date()函数格式化字符:

  • d - 月份日期(0 - 31)的数字表示
  • m - 一年中月份的数字表示(1 - 12)
  • y - 一年(99,00,01,02)的数字表示(两位数)
  • H - 24小时格式当前小时(00 - 23)
  • i - 分钟(00 - 59)
  • s - 秒(00 - 59)

答案 3 :(得分:0)

if(date('j') == '1' && date('n') == 1){
    // if first day of year
}

如果没有您通常需要担心的复杂日期,这将很容易实现。

答案 4 :(得分:0)

有关更多信息,请参阅http://php.net/date

<?php
 if(date('dm',mktime())=="0101")
    echo "fist day of year";
 else
    echo "not the first day";
?>

答案 5 :(得分:0)

一年的第一天与1/1 / yyyy永远不同 您可以使用strcmp或

$now=new DateTime();

然后从$ now使用getDay GetMonth来评估

答案 6 :(得分:0)

如果您对一年中第一天的名字感兴趣

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

OR

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

答案 7 :(得分:0)

您可以执行以下操作:

echo date('Y-m-d', strtotime("first day of january " . date('Y')));

答案 8 :(得分:0)

date函数具有z格式字符,可用于此:

$day_of_year = date('z');
// $day_of_year is 259 for the day I write this