Php 按月份显示日期
我从数据库中选择了一些日期,并按月显示这些日期。。我使用下面的代码Php 按月份显示日期,php,mysql,date,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
$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;
}
我想像这样显示这些日期
January (3)
2012-01-16
2012-01-26
2012-01-27
February (1)
2012-02-02
March(2)
2012-03-21
2012-03-22
这可能吗?请帮助您可以这样做来获得您想要的东西 使用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;
}
}
这里有一些东西会起作用,但需要测试。您需要在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
}
您需要一个数组,其中键是完整的月份名称。你会想做这样的事情
下面是一个示例脚本:
样本输出
而不是在将日期添加到数组时打印日期,如
无论您在何处使用echo$test\u date apend来表示数组
$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>';
}
for($i=0;$i无法通过检查test_date、start_date和end_date来理解您在代码中做什么?要这样显示您为什么需要所有上述内容?可以通过查询来完成。$qsdate=date(“Y”)。-01-01;$qedate=date(“Y-m-d”);$start_date=strotime($qsdate);$end_date=strotime($$qedate);$test\u date=$start\u date;$day\u incrementer=1;$count\u leaves=0;$flag=0;我可以通过删除上述所有条件和您请求的格式来给出代码。您显示的日期来自数据库,对吗?我已经计算了员工的休假。表中包含了每年的所有工作日详细信息和假期r、 根据这些细节计算假期,也不包括周六和周日……这工作正常。错误在显示格式中。我只需要指出,此代码将与2037年或更晚的日期断开。这可以使用DateTime
进行修复。是的,但我怀疑它会持续那么久。A)你怎么可能知道?B) 如果OP正在计算未来的日期呢?如果(!array_key_exists($tmp_month,$result))这将是一个更好的选项,而不是is_array()。我只需要指出,此代码将在2037年或更晚的日期中断。可以使用DateTime
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"
}
}
$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>';
}