Search code examples
phptimestamplocalestrftime

using strftime for 31 december shows next year


Using this code,

   setlocale(LC_ALL, 'fr_FR');
    $end_date_formatted = utf8_encode(strftime("%d %B %G", mktime(0, 0, 0, $month_end, $day_end, $year_end)));

strftime is not returning the right year only for Dec 31. it should display 31 decembre 2013, but it displays 31 decembre 2014


Solution

  • You are were using mktime() wrong. It's:

    int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

    ... and you have:

    mktime(0, 0, 0, $day_end, $month_end, $year_end)
    

    Fixed that, you're probably hitting the issue that the note in the strftime() manual page warns about:

    Note: %G and %V, which are based on ISO 8601:1988 week numbers can give unexpected (albeit correct) results if the numbering system is not thoroughly understood. See %V examples in this manual page.

    31st Dec 2013 belongs to first ISO week of next year.

    You probably want %Y instead.