我肯定同意“框架”的解决办法,并同意“Eelviejo”的增补,帮助我向前迈进。 但我认为我遇到了一些问题,它们可能有助于其他人:(+如果我错的话,请更正我)。
- Important to note: both
$StartDate
and $StopDate
are of type timestamp
, not actually the date
-type (1). So I changed the function parameters to $myTimestamp
in the hope of not confuse others.
例如:
$StartDate = strtotime("Sept 2010");
$StopDate = time(); // current timestamp
- For the functions of @elviejo:
- typecasting: a simple
(int)
didn t work for me (PHP5), so I used intval()
instead
- working with
%
gives results ranging from 0 to 11, while months are in the range of 1 to 12 (2). So in the function GetDateFromMonths($months)
, we have to +1
the result
- Because we add 1 to the
%
in the function GetDateFromMonths($months)
, we will have to substract 1 from the $dateAsMonths
in the function GetMonthsFromDate($myTimestamp)
.
这给我带来了以下法典:
function GetMonthsFromDate($myTimestamp)
{
$year = (int) date( Y ,$myTimestamp);
$months = (int) date( m , $myTimestamp);
$dateAsMonths = 12*$year + $months - 1;
return $dateAsMonths;
}
function GetDateFromMonths($months)
{
$years = intval($months / 12);
$month = intval($months % 12) + 1;
$myTimestamp = strtotime("$years/$month/01"); //makes a date like 2009/12/01, and translate it to a timestamp
return $myTimestamp;
}
由以下<条码>启动:
for ($i = GetMonthsFromDate($StartDate); $i <= GetMonthsFromDate($StopDate); $i++)
{
echo(date( d M Y ,GetDateFromMonths($i))."</br>");
}
哪是想要的结果。 我的发言是否有意义?
注:
中,但我可能错。
<<(2)> January is 1, February is 2, ...... November is 11 but December is 0. 这不会带来希望的结果。