Re: Re: Previous and Next Month and Year

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Vlad,

Thank you for taking the time to help me.

The code:
 $prev_month = date('F Y', mktime(0, 0, 0, 0, date('m') - 6, date('Y')));
 $next_month = date('F Y', mktime(0, 0, 0, 0, date('m') + 6, date('Y')));

 echo "$prev_month <br /> \n";
 echo "$next_month <br /> \n";

Generates:
November 2006 December 2006
The $prev_month value seems correct,
but the $next_month value should be:
 December 2008

Do you see my mistake?


Keith

----- Original Message ----- From: "Vlad Vlasceanu" <vvvlad@xxxxxxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Sent: Sunday, June 17, 2007 12:13 AM
Subject:  Re: Previous and Next Month and Year


time() returns a unix timestamp of the current moment in time (now), so in effect you are adding or subtracting 6 seconds to that.
calling:
date('m/d/Y', time());
is the same as calling:
date('m/d/Y');

On the other hand:
mktime(hour, minute, second, month, day, year) generates a timestamp such that:
mktime(0, 0, 0, date('d'), date('m') + 6, date('Y'));
would generate a date timestamp 6 months in the future.
or
mktime(0, 0, 0, 0, date('m'), date('Y'));
would generate last day from last month this year.

Word of wisdom: don't rely solely on timestamp calculations e.g. now + 24 hours and so on, because you may need to take into account daylight savings times and such otherwise your calculations may be off.

Vlad

Keith Spiller wrote:
Hi,

RE: Previous and Next Month and Year This is my code:

 $next_month = date('F Y', time()+$month);
 $prev_month = date('F Y', time()-$month);

 echo "$prev_month <br /> \n";  #Result "June 2007"
 echo "$next_month <br /> \n";  #Result "June 2007"

What I am trying to do is get the month and year
based upon a + or - $month value.

The variable $month being "6" for the month of June of
the present year of 2007. For example ($month -7) would be December 2006,
and ($month +7) would be January 2006.

Please help...  Thank you.

Keith

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


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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux