如何查看今天的天气是一年中的第一天或不是在PHP。
编辑: 回音日期('Y-m-d',strToTime('今年'1/1'));
这段代码给了我结果。这是有效还是有更好的方式。
答案 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.'
}
答案 2 :(得分:0)
This should solve your problem and more!
<?php
$theDate = date("md");
if ($theDate=="0101") echo "It's A New Year!";
?>
Date()函数格式化字符:
答案 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
答案 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