gpt4 book ai didi

php - 按月显示日期

转载 作者:行者123 更新时间:2023-11-29 08:51:39 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com