php - 按月显示日期

标签 php mysql date

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

$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 

这可能吗?请帮忙

最佳答案

这是可以工作但需要测试的东西。您需要将逻辑插入 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()(请参阅下面的评论)。

关于php - 按月显示日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11030200/

相关文章:

php - 将 MySQL 查询转换为对象的最佳方法

javascript - 使用 PHP 和 javascript 数组的 Google 图表

javascript - 使用实时数据在谷歌地图上移动标记

mysql - 如何检测 vb.net 和 mysql 数据库中的重复条目

javascript - 以 "MM yy"格式设置 jQuery UI Datepicker 的日期

php - 如何测试php的可扩展性

mysql - 如何计算JPA查询中的行表

php - 如何安全地使用 MySQLi?

python - 获取python中两个日期之间日期的星期数

java - @RequestBody 中的日期问题