복붙노트

이 날짜의 마지막 날을 찾는 방법은 무엇입니까?

PHP

이 날짜의 마지막 날을 찾는 방법은 무엇입니까?

PHP로 달의 마지막 날을 얻으려면 어떻게해야합니까?

주어진:

$a_date = "2009-11-23"

나는 2009-11-30을 원한다. 주어진

$a_date = "2009-12-23"

나는 2009-12-31 싶다.

해결법

  1. ==============================

    1.t는 주어진 날짜의 달의 일 수를 반환합니다 (문서의 날짜 참조) :

    t는 주어진 날짜의 달의 일 수를 반환합니다 (문서의 날짜 참조) :

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

    2.strtotime ()을 사용하는 코드는 2038 년 이후에 실패합니다. (이 스레드의 첫 번째 대답 에서처럼) 예를 들어 다음을 사용하십시오.

    strtotime ()을 사용하는 코드는 2038 년 이후에 실패합니다. (이 스레드의 첫 번째 대답 에서처럼) 예를 들어 다음을 사용하십시오.

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

    그것은 답을 줄 것이다 : 1970-01-31

    따라서 strtotime 대신 DateTime 함수를 사용해야합니다. 다음 코드는 2038 년 문제없이 작동합니다.

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

    3.나는 이것이 조금 늦다는 것을 알고 있지만, PHP 5.3+에서 DateTime 클래스를 사용하여보다 우아한 방법이 있다고 생각한다.

    나는 이것이 조금 늦다는 것을 알고 있지만, PHP 5.3+에서 DateTime 클래스를 사용하여보다 우아한 방법이 있다고 생각한다.

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

    4.PHP 함수 cal_days_in_month ()가 내장되어 있습니까?

    PHP 함수 cal_days_in_month ()가 내장되어 있습니까?

    echo cal_days_in_month(CAL_GREGORIAN, 11, 2009); 
    // = 30
    
  5. ==============================

    5.이 작동합니다.

    이 작동합니다.

    $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).'<br/>';
    echo date('D, M jS Y', $week_end).'<br/>';
    
    echo date('D, M jS Y', $month_start).'<br/>';
    echo date('D, M jS Y', $month_end).'<br/>';
    
    echo date('D, M jS Y', $year_start).'<br/>';
    echo date('D, M jS Y', $year_end).'<br/>';
    
  6. ==============================

    6.다음 달 첫 번째 날짜를 만든 다음 strtotime ( "- 1 day", $ firstOfNextMonth)을 사용하여 다음 달의 날짜를 만들 수 있습니다.

    다음 달 첫 번째 날짜를 만든 다음 strtotime ( "- 1 day", $ firstOfNextMonth)을 사용하여 다음 달의 날짜를 만들 수 있습니다.

  7. ==============================

    7.뭐가 잘못 되었 니 - 가장 우아한 날은 DateTime을 사용하는 것입니다.

    뭐가 잘못 되었 니 - 가장 우아한 날은 DateTime을 사용하는 것입니다.

    나는 DateTime :: createFromFormat, one-liner를 보지 않는다고 생각한다.

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

    8.귀하의 솔루션은 여기에 있습니다 ..

    귀하의 솔루션은 여기에 있습니다 ..

    $lastday = date('t',strtotime('today'));
    
  9. ==============================

    9.PHP 5.3 이상을 사용하고 있다면 이것을 시도해보십시오.

    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');
    

    등등..

  10. ==============================

    10.PHP DateTime에 Carbon API 확장을 사용하면 다음과 같이 월말을 사용할 수 있습니다.

    PHP DateTime에 Carbon API 확장을 사용하면 다음과 같이 월말을 사용할 수 있습니다.

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

    11.datetime과 함께 사용할 수도 있습니다.

    datetime과 함께 사용할 수도 있습니다.

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

    12.당신은 달의 마지막 날을 여러 가지 방법으로 찾을 수 있습니다. 하지만 PHP strtotime ()과 date () 함수를 사용하여이 작업을 수행 할 수 있습니다. 최종 코드가 다음과 같이 보일 것이라고 생각합니다.

    당신은 달의 마지막 날을 여러 가지 방법으로 찾을 수 있습니다. 하지만 PHP strtotime ()과 date () 함수를 사용하여이 작업을 수행 할 수 있습니다. 최종 코드가 다음과 같이 보일 것이라고 생각합니다.

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

    라이브 데모

    그러나 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');
    

    라이브 데모

    또한 다음과 같은 함수를 사용하여이를 해결할 수 있습니다.

    /**
     * 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);
    
  13. ==============================

    13.

    $date1 = $year.'-'.$month; 
    $d = date_create_from_format('Y-m',$date1); 
    $last_day = date_format($d, 't');
    
  14. ==============================

    14.다음은 완전한 기능입니다.

    다음은 완전한 기능입니다.

    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

  15. ==============================

    15.달의 마지막 날을 얻는 방법이 있습니다.

    달의 마지막 날을 얻는 방법이 있습니다.

    //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]); 
    
  16. ==============================

    16.Zend_Date를 사용하면 꽤 쉽습니다.

    Zend_Date를 사용하면 꽤 쉽습니다.

    $date->setDay($date->get(Zend_Date::MONTH_DAYS));
    
  17. ==============================

    17.나는 늦었지만 언급 한 것처럼 쉽게 할 수있는 몇 가지 방법이있다.

    나는 늦었지만 언급 한 것처럼 쉽게 할 수있는 몇 가지 방법이있다.

    $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이 가장 견고한 방법은 아니며, 다용도가 쉽지 않은 경우는 거의 없다고 말했습니다.

  18. ==============================

    18.

    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

  19. ==============================

    19.내 데이트 시간 도우미 수업에 여기 래핑했습니다. https://github.com/normandqq/Date-Time-Helper ~을 사용하여     $ dateLastDay = Model_DTHpr :: getLastDayOfTheMonth ();

    내 데이트 시간 도우미 수업에 여기 래핑했습니다. https://github.com/normandqq/Date-Time-Helper ~을 사용하여     $ dateLastDay = Model_DTHpr :: getLastDayOfTheMonth ();

    그리고 그것은 끝났다.

  20. ==============================

    20.mktime을 사용하고 날짜가 아닌 다른 방법 ( 't') :

    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인지 계산합니다.

  21. ==============================

    21.PHP DateTime을위한 Carbon API 확장

    PHP DateTime을위한 Carbon API 확장

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

    또는

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

    돌아올거야.

    30
    
  22. ==============================

    22.이 달의 끝을 훨씬 우아한 방법 :

    이 달의 끝을 훨씬 우아한 방법 :

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

    23.특정 날짜에 날짜 기능을 사용하려면 "t"를 사용하십시오.

    특정 날짜에 날짜 기능을 사용하려면 "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";
    

    코드는 스스로 설명됩니다. 그래서 도움이되기를 바랍니다.

  24. from https://stackoverflow.com/questions/1686724/how-to-find-the-last-day-of-the-month-from-date by cc-by-sa and MIT license