首页 文章

如何找到该日期的最后一天?

提问于
浏览
312

如何在PHP中获取本月的最后一天?

鉴于:

$a_date = "2009-11-23"

我想要2009-11-30;给定

$a_date = "2009-12-23"

我想2009-12-31 .

23 回答

  • -3

    我知道这有点晚了但我认为使用DateTime类使用 PHP 5.3+ 有更优雅的方法:

    $date = new DateTime('now');
    $date->modify('last day of this month');
    echo $date->format('Y-m-d');
    
  • 17

    t 返回给定日期的月份天数(请参阅the docs for date):

    $a_date = "2009-11-23";
    echo date("Y-m-t", strtotime($a_date));
    
  • 668

    使用strtotime()的代码将在2038年后失败 . (如此线程中的第一个答案所示)例如,尝试使用以下代码:

    $a_date = "2040-11-23";
    echo date("Y-m-t", strtotime($a_date));
    

    它将给出答案:1970-01-31

    因此,应该使用DateTime函数而不是strtotime . 以下代码将在没有2038年问题的情况下运行:

    $d = new DateTime( '2040-11-23' ); 
    echo $d->format( 'Y-m-t' );
    
  • 95

    还有内置的PHP函数 cal_days_in_month()

    “此功能将返回指定日历的一年中的天数 . ” http://php.net/manual/en/function.cal-days-in-month .

    echo cal_days_in_month(CAL_GREGORIAN, 11, 2009); 
    // = 30
    
  • 59

    这应该工作:

    $week_start = strtotime('last Sunday', time());
    $week_end = strtotime('next Sunday', time());
    
    $month_start = strtotime('first day of this month', time());
    $month_end = strtotime('last day of this month', time());
    
    $year_start = strtotime('first day of January', time());
    $year_end = strtotime('last day of December', time());
    
    echo date('D, M jS Y', $week_start).'
    '; echo date('D, M jS Y', $week_end).'
    '; echo date('D, M jS Y', $month_start).'
    '; echo date('D, M jS Y', $month_end).'
    '; echo date('D, M jS Y', $year_start).'
    '; echo date('D, M jS Y', $year_end).'
    ';
  • 7

    您可以为下个月的第一天创建日期,然后使用 strtotime("-1 day", $firstOfNextMonth)

  • 1

    有什么不对 - 对我来说最优雅的是使用DateTime

    我不知道我没看到DateTime::createFromFormat,单行

    $lastDay = \DateTime::createFromFormat("Y-m-d", "2009-11-23")->format("Y-m-t");
    
  • 12

    你的解决方案在这里..

    $lastday = date('t',strtotime('today'));
    
  • 1

    试试这个,如果您使用的是PHP 5.3,

    $a_date = "2009-11-23";
    $date = new DateTime($a_date);
    $date->modify('last day of this month');
    echo $date->format('Y-m-d');
    

    要查找下个月的最后日期,请修改如下,

    $date->modify('last day of 1 month');
     echo $date->format('Y-m-d');
    

    等等..

  • 4

    如果您对PHP DateTime使用Carbon API扩展,则可以使用以下命令获取该月的最后一天:

    $date = Carbon::now();
    $date->addMonth();
    $date->day = 0;
    echo $date->toDateString(); // use toDateTimeString() to get date and time
    
  • 0

    您也可以将它与日期时间一起使用

    $date = new \DateTime();
    $nbrDay = $date->format('t');
    $lastDay = $date->format('Y-m-t');
    
  • 9

    你可以找到几个月的最后一天 . 但只是你可以使用PHP strtotime()date()函数来做到这一点 . 我想你的最终代码看起来像这样:

    $a_date = "2009-11-23";
    echo date('Y-m-t',strtotime($a_date));
    

    Live Demo

    但是如果你使用PHP> = 5.2我强烈建议你使用新的DateTime对象 . 例如如下:

    $a_date = "2009-11-23";
    $date = new DateTime($a_date);
    $date->modify('last day of this month');
    echo $date->format('Y-m-d');
    

    Live Demo

    此外,您可以使用自己的功能解决此问题,如下所示:

    /**
     * Last date of a month of a year
     *
     * @param[in] $date - Integer. Default = Current Month
     *
     * @return Last date of the month and year in yyyy-mm-dd format
     */
    function last_day_of_the_month($date = '')
    {
        $month  = date('m', strtotime($date));
        $year   = date('Y', strtotime($date));
        $result = strtotime("{$year}-{$month}-01");
        $result = strtotime('-1 second', strtotime('+1 month', $result));
    
        return date('Y-m-d', $result);
    }
    
    $a_date = "2009-11-23";
    echo last_day_of_the_month($a_date);
    
  • 0
    $date1 = $year.'-'.$month; 
    $d = date_create_from_format('Y-m',$date1); 
    $last_day = date_format($d, 't');
    
  • 2

    使用Zend_Date非常简单:

    $date->setDay($date->get(Zend_Date::MONTH_DAYS));
    
  • 2

    这是一个完整的功能:

    public function get_number_of_days_in_month($month, $year) {
        // Using first day of the month, it doesn't really matter
        $date = $year."-".$month."-1";
        return date("t", strtotime($date));
    }
    

    这将输出如下:

    echo get_number_of_days_in_month(2,2014);
    

    产量:28

  • 4

    有办法获得一个月的最后一天 .

    //to get last day of current month
    echo date("t", strtotime('now'));
    
    //to get last day from specific date
    $date = "2014-07-24";
    echo date("t", strtotime($date));
    
    //to get last day from specific date by calendar
    $date = "2014-07-24";
    $dateArr=explode('-',$date);
    echo cal_days_in_month(CAL_GREGORIAN, $dateArr[1], $dateArr[0]);
    
  • 9

    我迟到了,但有一些简单的方法可以做到这一点:

    $days = date("t");
    $days = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
    $days = date("j",mktime (date("H"),date("i"),date("s"),(date("n")+1),0,date("Y")));
    

    使用mktime()是我完全控制时间的各个方面... I.E.

    echo "<br> ".date("Y-n-j",mktime (date("H"),date("i"),date("s"),(11+1),0,2009));
    

    将日期设置为0并将月份设置为1将为您提供上个月的最后一天 . 0和负数在不同的争论中具有相似的影响 . PHP: mktime - Manual

    正如少数人所说,strtotime不是最坚实的方式,如果没有那么容易多才多艺 .

  • 0
    function first_last_day($string, $first_last, $format) {
        $result = strtotime($string);
        $year = date('Y',$result);
        $month = date('m',$result);
        $result = strtotime("{$year}-{$month}-01");
        if ($first_last == 'last'){$result = strtotime('-1 second', strtotime('+1 month', $result)); }
        if ($format == 'unix'){return $result; }
        if ($format == 'standard'){return date('Y-m-d', $result); }
    }
    

    http://zkinformer.com/?p=134

  • 1

    我已将它包装在我的日期时间助手类中https://github.com/normandqq/Date-Time-Helper使用 $dateLastDay = Model_DTHpr::getLastDayOfTheMonth();

    它完成了

  • 0

    使用 mktime 而不是日期('t')的另一种方式:

    $dateStart= date("Y-m-d", mktime(0, 0, 0, 10, 1, 2016)); //2016-10-01
    $dateEnd = date("Y-m-d", mktime(0, 0, 0, 11, 0, 2016)); //This will return the last day of october, 2016-10-31 :)
    

    所以这样计算它是31,30还是29

  • -2

    Carbon PHP DateTime的API扩展

    Carbon::parse("2009-11-23")->lastOfMonth()->day;
    

    要么

    Carbon::createFromDate(2009, 11, 23)->lastOfMonth()->day;
    

    将重新开始

    30
    
  • 77

    这是一个更优雅的方式来结束这个月:

    $thedate = Date('m/d/Y'); 
      $lastDayOfMOnth = date('d', mktime(0,0,0, date('m', strtotime($thedate))+1, 0, date('Y', strtotime($thedate))));
    
  • 23

    您可以在日期函数中使用“ t ”来获取特定月份中的天数 .

    代码将是这样的:

    function lastDateOfMonth($Month, $Year=-1) {
        if ($Year < 0) $Year = 0+date("Y");
        $aMonth         = mktime(0, 0, 0, $Month, 1, $Year);
        $NumOfDay       = 0+date("t", $aMonth);
        $LastDayOfMonth = mktime(0, 0, 0, $Month, $NumOfDay, $Year);
        return $LastDayOfMonth;
    }
    
    for($Month = 1; $Month <= 12; $Month++)
        echo date("Y-n-j", lastDateOfMonth($Month))."\n";
    

    代码是自我解释的 . 所以希望它有所帮助 .

相关问题