0

我从数据库中选择了一些日期并按月显示这些日期。我正在使用以下代码

$work_res = mysql_query("(SELECT DISTINCT date FROM `work_details` WHERE  employee_id='" . $emp_id . "' and date between  '" . $qsdate . "' and '" . $qedate . "') UNION (SELECT holy_date from holiday where holy_date between  '" . $qsdate . "' and '" . $qedate . "')");


    while ($row = mysql_fetch_array($work_res)) {
 echo date("F", $test_date).'<br>';
        while ((date("Y-m-d", $test_date) < $row['date']) && ($flag = 0)) {

            if (!(date('N', strtotime(date("Y-m-d", $test_date))) >= 6)) {

                echo "<tr ><td align=center class=fontclass style=color:FF0000>" . date("Y-m-d F", $test_date) . "</td></tr>";
            }
            $test_date = $test_date + ($day_incrementer * 60 * 60 * 24);
        }
        $flag = 1;


        while ((date("Y-m-d", $test_date) != $row['date'])) {

            if (!(date('N', strtotime(date("Y-m-d", $test_date))) >= 6)) {
                echo "<tr><td align=center class=fontclass style=color:FF0000>" . date("Y-m-d F", $test_date) . "</td></tr>";
            }
            $test_date = $test_date + ($day_incrementer * 60 * 60 * 24);
        }
        $test_date = $test_date + ($day_incrementer * 60 * 60 * 24);
    }


    while (date("Y-m-d", $test_date) <= date("Y-m-d", $end_date)) {
        if (!(date('N', strtotime(date("Y-m-d", $test_date))) >= 6)) {
            echo "<tr><td align=center class=fontclass style=color:FF0000>" . date("Y-m-d F", $test_date) . "</td></tr>";
        }
        $test_date = $test_date + ($day_incrementer * 60 * 60 * 24);
    }


    return;
}

得到像这样的结果

2012-01-16 January
2012-01-26 January
2012-01-27 January
2012-02-02 February
2012-03-21 March
2012-03-22 March

我想显示这些日期

 January (3)
    2012-01-16
    2012-01-26
    2012-01-27
February (1)
    2012-02-02 
March(2)
    2012-03-21 
    2012-03-22 

这可能吗?请帮忙

4

4 回答 4

1

这是可行但需要测试的东西。您需要在 dateIsValid()函数中插入您的逻辑。

<?php

$work_res = mysql_query("(SELECT DISTINCT date FROM `work_details` WHERE  employee_id='" . $emp_id . "' and date between  '" . $qsdate . "' and '" . $qedate . "') UNION (SELECT holy_date from holiday where holy_date between  '" . $qsdate . "' and '" . $qedate . "')");


 //Group all dates into their Months
 while($result =  @mysql_fetch_array($work_res))
  {
    $date = $result['date'];
    $month = date("F",$date );

    //Your complex logic in between...
    if(dateIsValid())
       $output[$month][] = date("Y-m-d", $date);
  }

 //Display as required
 foreach($output as $month => $dates)
  {
    echo $month." (".count($dates).")"; //Echo the month heading
    foreach($dates as $date) echo $date; //Echo the date
  }

备注:

  1. 这种分组在 SQL 中是可能的,并且很可能应该在那里完成。
  2. 将所有逻辑移至单独的函数
  3. 对于数据库访问,使用 PHP 的PDO API或像 redBean 这样的 ORM
  4. 在你的代码中写注释 注释应该解释已经实现了什么逻辑。
  5. 使用更好的变量名。
  6. 使用 DateTime 而不是 date() (见下面的评论)。
于 2012-06-14T10:06:13.467 回答
1

您需要一个数组,其中键是完整的月份名称。你会想做这样的事情......


这是一个示例脚本:http ://codepad.org/pJOpDr17

$dateArray=array();

while (($row = mysql_fetch_array($work_res)){
    $d = new DateTime($row['date']);
    $monthName = $d->format('F');
    //$monthName = $d->format('F_Y'); (If you want month_year)
    $dateArray[$monthName][] = $row['date'];
}

样本输出

array(3) {
  ["January"]=>
  array(3) {
    [0]=>
    string(10) "2012-01-16"
    [1]=>
    string(10) "2012-01-26"
    [2]=>
    string(10) "2012-01-27"
  }
  ["February"]=>
  array(1) {
    [0]=>
    string(10) "2012-02-02"
  }
  ["March"]=>
  array(2) {
    [0]=>
    string(10) "2012-03-21"
    [1]=>
    string(10) "2012-03-22"
  }
}
于 2012-06-14T10:12:03.647 回答
0

而不是在将日期添加到数组时打印日期,例如

无论您在哪里使用 echo $test_date 附加到一个数组说

$leavesTakes[]=$date("Y-m-d F", $test_date)

现在使用循环显示日期

for($i=0;$i<count($leavesTakes);$i++)
{
  $cnt=0;
  for($j=$i;$j<count($leavestaken);$j++)
  {
    if($leavesTaken[$i]!=$leavesTaken[$j])
        break;

     $cnt++;
  }

  //print month name
  echo '<tr><td> month name (' + $cnt + ')'; //print month name

  for(;$i<$j;$i++)        
    echo '<br/>$date("Y-m-d F", $leavesTaken[$i]);

   echo '</td></tr>';

}

我不知道从日期打印月份名称的格式。所以请忽略打印月份名称并替换为实际的php函数来打印月份名称

于 2012-06-14T10:19:27.377 回答
0

你可以做这样的事情来得到你正在寻找的东西

像 Dutchie432 建议的那样使用 DateTime

result = array();
while ($row = mysql_fetch_array($work_res))
{
  $tmp_time = strtotime(row['date']);
  $tmp_month = date('F',$tmp_time);
  $tmp_date = date('Y-m-d', $tmp_time);
  if(!is_array($result[$tmp_month]))
  {
    $result[$tmp_month] = array();
  }
  array_push(result[$tmp_month], $tmp_date);
}

# this will print something similar to your req
foreach($result as $month => $dates)
{
  echo $month . " (" . $result[$month].length . ")";
  foreach($dates as $date)
  {
    echo $date;
  }
}
于 2012-06-14T09:57:07.810 回答