【问题标题】:How to check for the 1st day of an year in php如何在php中检查一年中的第一天
【发布时间】:2012-08-13 15:31:30
【问题描述】:

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

编辑: echo date('Y-m-d', strToTime('今年1/1'));

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

【问题讨论】:

  • 第一天总是 01/01 那么为什么不检查第一个月的第一天呢? (除非我们被外星人或其他人征服,否则他们可能会先说 02/04 之类的)
  • 我想他想知道每年 1 月 1 日的工作日名称是什么。

标签: php date


【解决方案1】:

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

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

【讨论】:

  • 非常感谢 Janis,这正是我想要的。
【解决方案2】:
if (date('z') === 0) {
echo 'we have 1.1.'
}

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

【讨论】:

    【解决方案3】:
    if(date('j') == '1' && date('n') == 1){
        // if first day of year
    }
    

    这将很容易做到,而无需您通常担心的任何复杂的日期。

    【讨论】:

      【解决方案4】:

      欲了解更多信息,请参阅http://php.net/date

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

      【讨论】:

        【解决方案5】:

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

        $now=new DateTime();
        

        然后使用 $now 中的 getDay GetMonth 进行评估

        【讨论】:

          【解决方案6】:

          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)

          【讨论】:

          • @Sammaye:如果你向下滚动,有脚本可以找到日期和日期!
          • 从 strtotime 函数来看,我仍然看不到它是如何回答问题的
          • 感谢各位早日回复。实际上,我想在一年中的第一天(1 月 1 日)运行一个 php 脚本。因此,我想验证今天的天气是一年中的第 1 天。感谢您的想法和链接。我会检查一下。
          • @nirosharathnayaka 你应该研究 cronjob 调度,这可以在没有 PHP 的情况下完成
          • 谢谢 Sammaye,我在 windows 环境下工作。我对cron一无所知。调度任务会做同样的事情吗?还是手动允许用户单击链接是否很好?如果脚本或其他东西失败了,那么如何再次自动重做工作?
          【解决方案7】:

          您可以执行以下操作:

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

          【讨论】:

            【解决方案8】:

            如果你对一年中第一天的名称感兴趣

            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

            【讨论】:

              【解决方案9】:

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-12-19
                • 1970-01-01
                • 2018-10-12
                • 1970-01-01
                • 2021-03-11
                • 2017-08-19
                相关资源
                最近更新 更多