Re: Function mktime() documentation question

From: Date: Sat, 10 Mar 2012 17:20:53 +0000
Subject: Re: Function mktime() documentation question
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-316967@lists.php.net to get a copy of this message
On 09-03-2012 14:11, Daniel Brown wrote:
     (To the list, as well.  First day with my new fingers, apparently....)
On Fri, Mar 9, 2012 at 08:09, Daniel Brown<danbrown@php.net> wrote:
On Thu, Mar 8, 2012 at 21:23, Tedd Sperling<tedd.sperling@gmail.com> wrote:
    This starts getting a bit off-topic from your original email, but
knowing that you're trying to use it for teaching your classes at the college, it may be of some value to you.
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.
    Sure it does, though you may have some issues when using
punctuation, unnecessary words, or using capital letters for anything other than proper names. What version of PHP are you using? I get the correct answers for all of the following phrases:
        "last day of April 2014"
        "last day of this month"
        "last day of next month"
        "last day of last month"
        "third Saturday March 2012"
    Or you can even be excruciatingly redundant:
        echo date('d',strtotime('last day of this
month',strtotime('next month')));
        echo date('d',strtotime('last day of this
month',strtotime('February 2018')));
        echo date('d',strtotime('second Monday',strtotime('September 2012')));
I must admit I'm still at a loss why people would want a function to tell them the amount of days in a month. That amount is pretty much fixed (except for february, but that's also mathematically easy to fix). So a simple function like: function getAmountOfDaysInAMonth($month, $year) { $days = array(31, (($year%4==0 and ($year%100 > 0 or $year%400==0)) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); return $days[$month+1]; } Would work just fine. Unless of course you want to count the amount of days during the changing of calendars (ie during the change of the julian calendar to the gregorian), or in different calendars altogether. Why (ab)use the datetime library for such a very simple thing? In the last case, I would indeed use DateTime, simply because it's not an easy answer. - Tul

« previous php.general (#316967) next »