FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » mktime() changes days when it should't
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: mktime() changes days when it should't [message #172468 is a reply to message #172414] Sat, 19 February 2011 23:53 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
TAXI wrote:
^^^^
Who?

> I write this couse of http://bugs.php.net/bug.php?id=54005 - As you can
> see Derik says it's not a bug (in PHP itself) and links me to here for
> support.
>
> P.S. Couse of Deriks last sentence I changed the script so that it
> passes 0 as hour to mktime() but that doesn't change anything.

Your approach is bogus, indeed: It will in at least 11 of 12 cases not yield
the date of the last day of the month, since there are months with 28, 29
(February), 30 (April, June, September, November), and 31 days (the rest)¹;
but 1 + 28, for which you could have simply passed 29, will only ever yield
a date of 29 (no overflow) or 1 (overflow in February in a non-leap year).

To determine the last day of a month (which is what you *really* wanted to
do, but did not ask), pass 0 for the day-of-month of the *next* month, then
retrieve the date:

/* 1970 CE was not a leap year, since it was not divisible by 4: "28" */
echo date('d', mktime(0, 0, 0, 3, 0, 1970)) . "\n";

/*
* 2000 was a leap year, since it was divisible by 4 and by 100,
* but also by 400: "29"
*/
echo date('d', mktime(0, 0, 0, 3, 0, 2000)) . "\n";

/* and so was 2004 (divisible by 4), so 2004-02 also had "29" days */
echo date('d', mktime(0, 0, 0, 3, 0, 2004)) . "\n";

/*
* 2100 will not be a leap year, since it is divisible by 4 and by 100,
* but not by 400; but dates after 2038-01-19 03:14:07.999 UTC are
* unsupported by 32-bit timestamps so the result is necessarily wrong:
* "01"
*/
echo date('d', mktime(0, 0, 0, 3, 0, 2100)) . "\n";

RTFM: <http://php.net/mktime>


PointedEars
___________
¹ Whenever you are unsure which are which, there is ─ quite literally ─
a rule of thumb I have learned as a child: Make a fist and count the
months along the ridges of the hands, from the left to right. The peaks
represent the months with 31 days, the valleys with 30 days or less.
(So the peaks at the index fingers represent July and August, both of
which have 31 days.)
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Almost done - Just need a way to assign a variable
Next Topic: Grate opportunities to earn money from online!
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Oct 05 21:42:23 GMT 2024

Total time taken to generate the page: 0.03686 seconds