【问题标题】:List of all months and year between two dates in PHPPHP中两个日期之间的所有月份和年份的列表
【发布时间】:2009-09-19 18:27:04
【问题描述】:

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

我想要一个基于月份和年份差异的月份和年份列表。

我正在实现基于年份开始月份和年份结束月份的搜索功能。 所以!

开始选择 - 01(月)-2009(年)

结束选择 10(月)-2009(年)

我对 MySQL 的要求是:

Month                        Year
JAN                          2009
FEB                          2009
MAR                          2009
APR                          2009
MAY                          2009
JUN                          2009
JUL                          2009
AUG                          2009
SEP                          2009
OCT                          2009

【问题讨论】:

  • 对我来说似乎是一个家庭作业问题 :)

标签: php mysql date


【解决方案1】:

FractalizeR 的答案是正确的。

让我通过定义函数来扩展:

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:试图发表评论,但格式搞砸了。 (当然这个函数可以重写为一个衬里,但希望更具可读性)

【讨论】:

  • 很棒的函数,虽然在 GetDateFromMonths 中,使用 (int) $months / 12 返回一个浮点数,因此在 strtotime 函数中不可用。我只是通过替换 $years = (int) ($months / 12); $month = (int) ($months % 12); 来完成这项工作
  • 感谢您提供的功能,但我确实遇到了一些问题,我已将(我的)解决方案放在另一个答案中
【解决方案2】:

您需要编写一些函数来将您的日期转换为自某个日期以来经过的月份数并返回。例如,自 1980 年 1 月以来。

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

然后你只需做简单的“for”循环:

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

【讨论】:

  • 这是这个问题的正确答案。让我通过定义函数来扩展: function GetMonthsFromDate ($myDate) { $year = (int) date('Y',$myDate); $months = (int) date('m', $myDate); $dateAsMonths = 12*$year +months;返回 $dateMonths; } 函数 GetDateFromMonths ($months) { $years = (int) $months / 12; $month = (int) $months % 12; $myDate = strtotime("$years/$month/1); return $myDate; } (当然这个函数可以重写为一个衬里,但希望更具可读性)
【解决方案3】:

虽然 FractalizeR 的答案是正确的。 还有另一种选择。

利用 strtotime('2009/08/01 - 1 month') 会做正确的事情并删除 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');
}

再次列出月份

【讨论】:

  • 是的,我认为这是完美的,可以快速完成工作的好东西。
  • 实际上,我更喜欢这个而不是 FractalizeR 的解决方案,这应该是正确的答案。更直接的代码,易于适应其他用途(只需跳 2 个月、三个月、几年……)
【解决方案4】:

这是最终的答案,效果很好

$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));
         }

【讨论】:

  • 这是一个糟糕但可能有效的解决方案。它涉及发出 SQL 查询,这很耗时。最好用 PHP 做所有事情
【解决方案5】:

我根据你们的意见编写了这个函数。看看这个:

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;
    }
}

【讨论】:

    【解决方案6】:

    您的逻辑非常适合获取月份列表。但不确定在这种情况下我们如何处理多年。

    这是我的代码:

    $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
    }
    

    【讨论】:

      【解决方案7】:

      我绝对同意@FractalizeR 的解决方案和@elviejo 的补充,它帮助我前进。但我相信我遇到了一些问题,它们可能对其他人有所帮助:(+如果我错了,请纠正我)

      1. 重要提示$StartDate$StopDate 都是 timestamp 类型,实际上不是 date 类型 (1)。所以我把函数参数改成了$myTimestamp,希望不要混淆别人。

      例如:

      $StartDate = strtotime("Sept 2010");
      $StopDate = time(); // current timestamp
      
      1. @elviejo 的功能:
        1. 类型转换:一个简单的(int) 不适合我(PHP5),所以我改用intval()
        2. 使用% 会得到从0 到11 的结果,而月份在1 到12 的范围内(2)。所以在函数GetDateFromMonths($months)中,我们要+1得到结果
        3. 因为我们在函数GetDateFromMonths($months)% 上加了1,所以我们必须从函数GetMonthsFromDate($myTimestamp)$dateAsMonths 中减去1。

      这给了我以下代码:

      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-loop 发起:

      for ($i = GetMonthsFromDate($StartDate); $i <= GetMonthsFromDate($StopDate); $i++)
      {
          echo(date('d M Y',GetDateFromMonths($i))."</br>");
      }
      

      这给出了想要的结果。我说的有道理吗?


      注意事项:

      (1) 我什至不认为date 作为type in PHP 存在,但我可能错了。

      (2) 1 月 1 日,2 月 2 日,... 11 月 11 日,但 12 月 0。这不会给出想要的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-29
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        • 2013-11-01
        • 2020-08-13
        相关资源
        最近更新 更多