php-general Digest 7 Mar 2012 21:29:07 -0000 Issue 7716

Topics (messages 316932 through 316934):

Function mktime() documentation question
        316932 by: Tedd Sperling
        316933 by: admin
        316934 by: Daniel Brown

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
Hi gang:

I am using the getdate(mktime()) functions to get month data (i.e., name of 
month, first weekday, last day, number of days).

To get the number of days for a specific month, I use:

// $current_month is the month under question

$next_month = $current_month + 1;       
$what_date = getdate(mktime(0, 0, 0, $next_month, 0, $year)); 
$days_in_current_month = $what_date['mday'];

That works for me!

However, if you read the documentation, namely:

http://php.net/manual/en/function.mktime.php

It states:

--- quote

day

The number of the day relative to the end of the previous month. Values 1 to 
28, 29, 30 or 31 (depending upon the month) reference the normal days in the 
relevant month. Values less than 1 (including negative values) reference the 
days in the previous month, so 0 is the last day of the previous month, -1 is 
the day before that, etc. Values greater than the number of days in the 
relevant month reference the appropriate day in the following month(s).
--- un-quote

From my code, the number of days in a month can be found by using 0 as the 
first index of the next month -- not the last day of the previous month.

As such, I would re-write the relevant portion of the paragraph to be:

day

The number of the day relative to the end of the previous month. Values 1 to 
28, 29, 30 or 31 (depending upon the month) reference the normal days in the 
relevant month. Values less than 0 reference the days in the previous month. 
For example, -1 is the day before the first day of the relevant month. The 
value 0 is the zero index of the next month, which is also equal to the last 
day of the relevant month. Values greater than zero are the number of days in 
the relevant month reference the appropriate day in the following month(s).

What say you?

Cheers,

tedd

_____________________
tedd.sperl...@gmail.com
http://sperling.com






--- End Message ---
--- Begin Message ---
Tedd,
        This area was always a little grey to me. 
I have used -1 to obtain the previous months for some time now.
0 always indicated the beginning index of the current month but the
explanation never seemed to fit the bill.


Having worked extensively in time manipulation in many of the development
projects I have come up with a rule of thumb.


$this_month = date('Y-m-d 00:00:00',mktime(0,0,0,date('m'),1,date('Y')));
$previous_month = date('Y-m-d
00:00:00',mktime(0,0,0,date('m')-1,1,date('Y')));
$next_month = date('Y-m-d 00:00:00',mktime(0,0,0,date('m')+1,1,date('Y')));

To get the days of any given month or just about anything you need to just
use the strtotime
$days_in_month = date('j',strtotime($this_month));






-----Original Message-----
From: Tedd Sperling [mailto:tedd.sperl...@gmail.com] 
Sent: Wednesday, March 07, 2012 3:04 PM
To: PHP-General List
Subject: [PHP] Function mktime() documentation question

Hi gang:

I am using the getdate(mktime()) functions to get month data (i.e., name of
month, first weekday, last day, number of days).

To get the number of days for a specific month, I use:

// $current_month is the month under question

$next_month = $current_month + 1;       
$what_date = getdate(mktime(0, 0, 0, $next_month, 0, $year));
$days_in_current_month = $what_date['mday'];

That works for me!

However, if you read the documentation, namely:

http://php.net/manual/en/function.mktime.php

It states:

--- quote

day

The number of the day relative to the end of the previous month. Values 1 to
28, 29, 30 or 31 (depending upon the month) reference the normal days in the
relevant month. Values less than 1 (including negative values) reference the
days in the previous month, so 0 is the last day of the previous month, -1
is the day before that, etc. Values greater than the number of days in the
relevant month reference the appropriate day in the following month(s).
--- un-quote

>From my code, the number of days in a month can be found by using 0 as the
first index of the next month -- not the last day of the previous month.

As such, I would re-write the relevant portion of the paragraph to be:

day

The number of the day relative to the end of the previous month. Values 1 to
28, 29, 30 or 31 (depending upon the month) reference the normal days in the
relevant month. Values less than 0 reference the days in the previous month.
For example, -1 is the day before the first day of the relevant month. The
value 0 is the zero index of the next month, which is also equal to the last
day of the relevant month. Values greater than zero are the number of days
in the relevant month reference the appropriate day in the following
month(s).

What say you?

Cheers,

tedd

_____________________
tedd.sperl...@gmail.com
http://sperling.com






--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php


--- End Message ---
--- Begin Message ---
On Wed, Mar 7, 2012 at 15:03, Tedd Sperling <tedd.sperl...@gmail.com> wrote:
> Hi gang:
>
> I am using the getdate(mktime()) functions to get month data (i.e., name of 
> month, first weekday, last day, number of days).
>
> To get the number of days for a specific month, I use:
>
> // $current_month is the month under question
>
> $next_month = $current_month + 1;
> $what_date = getdate(mktime(0, 0, 0, $next_month, 0, $year));
> $days_in_current_month = $what_date['mday'];
>
> That works for me!
>
> However, if you read the documentation, namely:
>
> http://php.net/manual/en/function.mktime.php
>
> It states:
>
> --- quote
>
> day
>
> The number of the day relative to the end of the previous month. Values 1 to 
> 28, 29, 30 or 31 (depending upon the month) reference the normal days in the 
> relevant month. Values less than 1 (including negative values) reference the 
> days in the previous month, so 0 is the last day of the previous month, -1 is 
> the day before that, etc. Values greater than the number of days in the 
> relevant month reference the appropriate day in the following month(s).
> --- un-quote
>
> From my code, the number of days in a month can be found by using 0 as the 
> first index of the next month -- not the last day of the previous month.

    I fail to follow.  Your code is looking ahead to next month
(April), then using the 0 day, which means it's getting the last day
(31) of the current month (March).  There's no such thing as a 0
April, hence anything less than one should count backward.

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

--- End Message ---

Reply via email to