WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Is this a bug? (date_i18n) (4 posts)

  1. alzuwaga
    Member
    Posted 1 year ago #

    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?

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    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.

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 1 year ago #

    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

  4. alzuwaga
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic