English 中文(简体)
PHP两个日期之间的所有月和年份清单
原标题:List of all months and year between two dates in PHP
  • 时间:2009-09-19 18:27:04
  •  标签:

我试图解释细节,以便容易理解。

我想根据月份和年份的差异列出一个月和年数。

I am implementing search functionality based on start month with year and end month with year. So!

<>开始甄选——01(月)-2009年(月)

www.un.org/Depts/DGACM/index_spanish.htm 最终选择10(月)-2009年(月)

我要从我的我的我的我的我的我的SQL那里得到的是:

Month                        Year
JAN                          2009
FEB                          2009
MAR                          2009
APR                          2009
MAY                          2009
JUN                          2009
JUL                          2009
AUG                          2009
SEP                          2009
OCT                          2009
最佳回答

分裂 答案是正确的。

让我扩大,界定职能:

function GetMonthsFromDate($myDate) {
  $year = (int) date( Y ,$myDate);
  $months = (int) date( m , $myDate);
  $dateAsMonths = 12*$year + $months;
  return $dateAsMonths;
}

function GetDateFromMonths($months) {
  $years = (int) $months / 12;
  $month = (int) $months % 12;
  $myDate = strtotime("$years/$month/01"); //makes a date like 2009/12/01
  return $myDate;
}

PS: tried to post as a comment but the formating got screwed. (Of course this functions could be rewritten as one liners but wanted to be more readable)

问题回答

你需要写一些职能,将日期改为自某一日期和之后数月。 例如,自1980年1月以来。

Jan 1980 = 0;
Dec 1980 = 12;
Jan 1981 = 13 etc.

然后,你只是简单地“为了”选择了:

for ($i = GetMonthsFromDate($StartDate), $i <= GetMonthsFromDate($StopDate), $i++) {
   echo(GetDateFromMonths($i));
}

Although FractalizeR answer is the correct one. There is another option.

利用这一时间(2009/08/01-1个月)将正确做事,删除一个月。

<?php
$startDate = strtotime("$startYear/$startMonth/01");
$endDate   = strtotime("$endYear/$endMonth/01");

$currentDate = $endDate;

while ($currentDate >= $startDate) {
    echo date( Y/m ,$currentDate);
    $currentDate = strtotime( date( Y/m/01/ ,$currentDate).  -1 month );
}

每月清单

这里的最后答案非常出色。

$startMonth= $_POST[ startmonth ];
    $startyear= $_POST[ startyear ];
    $cYear = $startyear;

    $endMonth= $_POST[ endmonth ];
    $endyear= $_POST[ endyear ];
    $sql = "SELECT PERIOD_DIFF(".$endyear.$endMonth.", ".$startyear.$startMonth.")";
    $queryRS =  $db->query($sql);
    $tmonthsarray = $db->fetchRow($c_jobsVat);
    $totalmonths=tmonthsarray[0];
         for($count=$startMonth; $count <= ($startMonth + $totalmonths);$count++)
         {  
            $processYear = $startyear + intval( ( $count - 1 ) / 12 );
        $processMonth= (( $count - 1 ) % 12 + 1);
        $processMonthName= date( F , mktime(0,0,0,$count));
         }

我根据你的意见撰写了这一职能。 具体如下:

function CountTheMonth($startDate,$endDate,$order)
{
    $startDate = strtotime($startDate);
    $endDate   = strtotime($endDate);

    $ASC_Month = $startDate;
    $DESC_Month = $endDate;
    $Y_Axis = Array();

    if($order ==  DESC )//Big to small
    {
        while ($DESC_Month >= $startDate) 
        {    
            $Y_Axis[] = date( F-Y ,$DESC_Month);
            $DESC_Month = strtotime( date( Y-m-d ,$DESC_Month).  -1 month );
        }
        return $Y_Axis;
    }
    elseif($order ==  ASC )//Small to big
    {
        while ($ASC_Month <= $endDate) 
        {    
            $Y_Axis[] = date( F-Y ,$ASC_Month);
            $ASC_Month = strtotime( date( Y-m-d ,$ASC_Month).  +1 month );
        }
        return $Y_Axis;
    }
}

你的逻辑非常有助于获得一个月清单。 但不能确定我们如何能够处理这种情况的年份。

我的守则是:

$startMonth= $_POST[ startmonth ];

$startyear= $_POST[ startyear ];

$endMonth= $_POST[ endmonth ];

$endyear= $_POST[ endyear ];

$sql = "SELECT PERIOD_DIFF(".$endyear.$endMonth.", ".$startyear.$startMonth.")";

$queryRS =  $db->query($sql);

$tmonthsarray = $db->fetchRow($c_jobsVat);

$totalmonths= $tmonthsarray[0]; 

for($count=$startMonth; $count <= ($startMonth + $totalmonths);$count++)
{
    echo "<BR>==>".date( F , mktime(0,0,0,$count)) ; // Months

    // what comes here in case of listing year
}

我肯定同意“框架”的解决办法,并同意“Eelviejo”的增补,帮助我向前迈进。 但我认为我遇到了一些问题,它们可能有助于其他人:(+如果我错的话,请更正我)。

  1. 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
  1. For the functions of @elviejo:
    1. typecasting: a simple (int) didn t work for me (PHP5), so I used intval() instead
    2. 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
    3. 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. 这不会带来希望的结果。





相关问题
热门标签