[thelist] PHP mkdate() question

Seb Potter seb at members.evolt.org
Mon Jul 16 18:00:53 CDT 2001


Chris Ditty sayeth:

| I am trying to get the number of days in the current month.  This is some
| code that I have been using for a few months.
|
| $smonth = date("n");
| $syear = date("Y");
| $days = date("d", mktime(0, 0, 0, $smonth + 1, 1, $syear) - 1); // Number
| of days in month
|
| For some reason, if I do not add the +1 to $smonth, it gets the date
| wrong.  Also, any reason for the -1?  Like I said, I don't remember where
I
| got this, but it has been working fine.  Just don't understand why.
|
| Anyone understand why?

Hmm.

What you're doing with $days is really, really complex.

You're getting the month from date("d") okay. Your mktime gets the value of
the first day of NEXT month (the +1), then regresses a day to get the value
of the last day of this month (the -1).

Ouchawawa.

For the number of days in the current month, try this:

$days = date("t");

from : http://www.php.net/manual/en/function.date.php

Works like a charm every time.

Seb
http://www.sebpotter.org





More information about the thelist mailing list