【问题标题】:How can I find the first and last date in a month using PHP?如何使用 PHP 找到一个月中的第一个和最后一个日期?
【发布时间】:2011-02-10 10:49:23
【问题描述】:

如何使用 PHP 找到一个月中的第一个和最后一个日期?例如,今天是 2010 年 4 月 21 日;我想查找 2010 年 4 月 1 日和 2010 年 4 月 30 日。

【问题讨论】:

标签: php date


【解决方案1】:

你可以使用DateTimeImmutable::modify()


$date = DateTimeImmutable::createFromFormat('Y-m-d', '2021-02-13');

var_dump($date->modify('first day of this month')->format('Y-m-d')); // string(10) "2021-02-01"
var_dump($date->modify('last day of this month')->format('Y-m-d')); // string(10) "2021-02-28"

【讨论】:

    【解决方案2】:

    如果你想从指定的日期变量中找到第一天和最后一天,那么你可以这样做:

    $date    =    '2012-02-12';//your given date
    
    $first_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", first day of this month");
    echo $first_date = date("Y-m-d",$first_date_find);
    
    $last_date_find = strtotime(date("Y-m-d", strtotime($date)) . ", last day of this month");
    echo $last_date = date("Y-m-d",$last_date_find);
    

    对于当前日期,只需简单地使用这个

    $first_date = date('Y-m-d',strtotime('first day of this month'));
    $last_date = date('Y-m-d',strtotime('last day of this month'));
    

    【讨论】:

    • 你可以用 $date 替换 date("Y-m-d", strtotime($date)) 因为它们是一样的但是你写的代码更少。
    【解决方案3】:

    简单格式

       $curMonth = date('F');
       $curYear  = date('Y');
       $timestamp    = strtotime($curMonth.' '.$curYear);
       $first_second = date('Y-m-01 00:00:00', $timestamp);
       $last_second  = date('Y-m-t 12:59:59', $timestamp); 
    

    下个月将 $curMonth 更改为 $curMonth = date('F',strtotime("+1 months"));

    【讨论】:

      【解决方案4】:
      $month=01;
      $year=2015;
      $num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
      echo $num;
      

      显示日期的最后一天 31

      【讨论】:

        【解决方案5】:

        获取Last Month的第一个和最后一个日期;

        $dateBegin = strtotime("first day of last month");  
        $dateEnd = strtotime("last day of last month");
        
        echo date("D-F-Y", $dateBegin);  
        echo "<br>";        
        echo date("D-F-Y", $dateEnd);
        

        【讨论】:

          【解决方案6】:

          对于特定的 monthyear 使用 date() 作为自然语言如下

          $first_date = date('d-m-Y',strtotime('first day of april 2010'));
          $last_date = date('d-m-Y',strtotime('last day of april 2010'));
          // Isn't it simple way?
          

          但对于当月

          $first_date = date('d-m-Y',strtotime('first day of this month'));
          $last_date = date('d-m-Y',strtotime('last day of this month'));
          

          【讨论】:

            【解决方案7】:

            最简单的方法是使用date,它允许您将硬编码值与从时间戳中提取的值混合。如果您不提供时间戳,则假定为当前日期和时间。

            // Current timestamp is assumed, so these find first and last day of THIS month
            $first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day
            $last_day_this_month  = date('m-t-Y');
            
            // With timestamp, this gets last day of April 2010
            $last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010'));
            

            date() 在给定的字符串(如'm-t-Y')中搜索特定符号,并将其替换为时间戳中的值。所以我们可以使用这些符号从时间戳中提取我们想要的值和格式。在上面的例子中:

            • Y 为您提供时间戳('2010')的 4 位数年份
            • m 为您提供时间戳中的数字月份,前导零 ('04')
            • t 为您提供时间戳所在月份的天数(“30”)

            您可以发挥创意。例如,要获取一个月的第一秒和最后一秒:

            $timestamp    = strtotime('February 2012');
            $first_second = date('m-01-Y 00:00:00', $timestamp);
            $last_second  = date('m-t-Y 12:59:59', $timestamp); // A leap year!
            

            有关其他符号和更多详细信息,请参阅http://php.net/manual/en/function.date.php

            【讨论】:

            • 只是在此处添加信息,我在使用这种格式将日期转换回 unix 时间戳时遇到问题,已通过使用 'dmY' 格式而不是 'mdY' 修复.. 最后一秒将是 '23: 59:59' 而不是 '12:59:59' ..
            【解决方案8】:

            简单的一个

            • Y - 年份的完整数字表示,4 位数字
            • m - 月份的数字表示,前导零
            • t - 给定月份的天数

            参考 - http://www.php.net/manual/en/function.date.php

            <?php
                echo 'First Date    = ' . date('Y-m-01') . '<br />';
                echo 'Last Date     = ' . date('Y-m-t')  . '<br />';
            ?>
            

            【讨论】:

            • 用任何月份数字替换 m 时不起作用
            【解决方案9】:

            您可以使用日期函数来查找一个月中有多少天。

            // Get the timestamp for the date/month in question.
            $ts = strtotime('April 2010');
            
            echo date('t', $ts); 
            // Result: 30, therefore, April 30, 2010 is the last day of that month.
            

            希望对您有所帮助。

            编辑:阅读 Luis 的回答后,我想到您可能希望它采用正确的格式 (YY-mm-dd)。这可能很明显,但提一下也无妨:

            // After the above code
            echo date('Y-m-t', $ts); 
            

            【讨论】:

              【解决方案10】:

              这将为您提供该月的最后一天:

              function lastday($month = '', $year = '') {
                 if (empty($month)) {
                    $month = date('m');
                 }
                 if (empty($year)) {
                    $year = date('Y');
                 }
                 $result = strtotime("{$year}-{$month}-01");
                 $result = strtotime('-1 second', strtotime('+1 month', $result));
                 return date('Y-m-d', $result);
              }
              

              第一天:

              function firstDay($month = '', $year = '')
              {
                  if (empty($month)) {
                    $month = date('m');
                 }
                 if (empty($year)) {
                    $year = date('Y');
                 }
                 $result = strtotime("{$year}-{$month}-01");
                 return date('Y-m-d', $result);
              } 
              

              【讨论】:

              • 这个功能对我有用。拯救了我的一天。
              猜你喜欢
              • 1970-01-01
              • 2012-05-29
              • 2013-06-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-12-13
              • 1970-01-01
              相关资源
              最近更新 更多