• Resolved alzuwaga

    (@alzuwaga)


    Hello. I have found something that may be a bug. And is related to this code.

    <?php
    include('wp-blog-header.php');
    echo date_i18n('F' ,strtotime('january')) . '<br>';
    echo date_i18n('F' ,strtotime('february')) . '<br>';
    echo date_i18n('F' ,strtotime('march')) . '<br>';
    echo date_i18n('F' ,strtotime('april')) . '<br>';
    echo date_i18n('F' ,strtotime('may')) . '<br>';
    echo date_i18n('F' ,strtotime('june')) . '<br>';
    echo date_i18n('F' ,strtotime('july')) . '<br>';
    echo date_i18n('F' ,strtotime('august')) . '<br>';
    echo date_i18n('F' ,strtotime('september')) . '<br>';
    echo date_i18n('F' ,strtotime('october')) . '<br>';
    echo date_i18n('F' ,strtotime('november')) . '<br>';
    echo date_i18n('F' ,strtotime('december')) . '<br>';
    ?>

    And these are the results. As of today (8/31/2012) returns:

    enero
    marzo
    marzo
    mayo
    mayo
    julio
    julio
    agosto
    octubre
    octubre
    diciembre
    diciembre

    Dated yesterday (8/30/2012) returns:

    enero
    marzo
    marzo
    abril
    mayo
    junio
    julio
    agosto
    septiembre
    octubre
    noviembre
    diciembre

    On tomorrow (9/1/2012) returns the right thing:

    enero
    febrero
    marzo
    abril
    mayo
    junio
    julio
    agosto
    septiembre
    octubre
    noviembre
    diciembre

    From what I see, it seems that the error occurs if the current day is superior in number to the last day of the following month, skipping it.

    What do you think?

Viewing 3 replies - 1 through 3 (of 3 total)
  • If you can replicate this whilst using Twenty Eleven with no active plugins, it could well be worth posting in Trac as a possible bug.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    This is not a bug, it’s just an unexpected result from the way you’re using strtotime().

    Saying strtotime('february') is not specific enough, so when you do that, PHP makes some assumptions. For one, it assumes the current year, but one thing you might not have expected is that it also assumes the current day.

    So on 8/30/2012, saying strtotime('february') is functionally equivalent to saying strtotime('february 30, 2012'). Which is of course nonsense, since February only has 28/29 days in it. So what you get back is actually the timestamp value for March 1st (since 2012 is a leap year), and thus the date_i18n() function is getting a timestamp of March 1st passed to it, so naturally it spits out “March”, in whatever language you’re translating to.

    Same thing happens on the 31st, but with more months, because some months only have 30 days in them.

    The fix: Be more specific. Instead of saying strtotime('february'), change it to strtotime('february 1') and similar. All months have a day 1. Or even better, use the mktime function instead, so as to be more specific about it without relying on the weird rules that strtotime has in certain cases.

    You’ll note that you get basically the same results if you just use date() instead of date_i18n(), which shows you that it isn’t a bug in date_i18n.

    A similar explanation can be found here:
    https://bugs.php.net/bug.php?id=49115

    Thread Starter alzuwaga

    (@alzuwaga)

    Ah, I see. It makes sense. Thanks Otto!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Is this a bug? (date_i18n)’ is closed to new replies.