Re: Function mktime() documentation question
| From: | Charles | Date: | Fri, 09 Mar 2012 02:51:20 +0000 |
| Subject: | Re: Function mktime() documentation question | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-316947@lists.php.net to get a copy of this message | ||
On Fri, Mar 9, 2012 at 9:23 AM, Tedd Sperling <tedd.sperling@gmail.com> wrote:
> On Mar 8, 2012, at 6:53 PM, Daniel Brown wrote:
> On Mar 8, 2012 6:14 PM, "Tedd Sperling" <tedd.sperling@gmail.com> wrote:
>>
>> > Side-point: I find it interesting that getdate() has all sorts of neat descriptions
>> > for the current month (such as, what weekday a numbered day is), but lacks how many days are in the
>> > month. Doesn't that seem odd?
>>
>> Oh, I see what you're saying now. Well, using getdate(), how else would you think to
>> pass the parameter to get the last day other than using the current month and the last day (which
>> would then obviously be overkill, of course).
>
> Well.. you could use any number that exceeds 31 -- or -- as I would have suggested if it had
> been up to me, zero day would provide the number of days in *that* month rather than the number of
> days in the previous month, which was the point of my post.
>
>> All of this aside, though, you may instead want to use something along the lines of
>> date('d',strtotime('last day of this month')); in tandem with your date
>> formatting.
>
> That's a good idea, but
>
>> date('d',strtotime('last day of this month'));
>
>
> gives me the number of days in *this* month, but not the next, or previous, month.
>
> I need the result to be whatever date was selected -- something like:
>
> $number_days = date('d',strtotime('last day of April, 2014'));
>
> But that doesn't work.
>
> You see, I need something that makes sense to students. The idea that you have to use the zero
> day (whatever that is) of the next month to see how many days there are in this month is strange and
> confusing -- again my point..
>
> Thus far, the following looks better than what I came up with::
>
> $what_date = getdate(mktime(0, 0, 0, $mon, 32, $year));
> $days_in_month = 32 - $what_date['mday'];
>
> But it's still strange.
>
> I was using:
>
> // get the last day of the month
> $cont = true;
> $tday = 27;
> while (($tday <= 32) && ($cont))
> {
> $tdate = getdate(mktime(0,0,0,$mon,$tday,$year));
> if ($tdate["mon"] != $mon)
> {
> $lastday = $tday - 1;
> $cont = false;
> }
> $tday++;
> }
>
> It made sense, but was too long. I figured there should be something better and easier to
> explain -- but I'm still looking.
function count_days($month, $year) { return (mktime(0, 0, 0, $month+1,
1, $year) - mktime(0, 0, 0, $month, 1, $year))/86400; }