Display dates in month wise

I've select some dates from the database and display these dates in month wise.. Am using the following code

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

Got the result like

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

I want to display these dates like

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

Is this possible? Please help

Answers


Here is something that will work but needs to be tested. You need to insert your logic in the dateIsValid() function.

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

Notes :

  1. This grouping is possible in SQL and should most probably done over there.
  2. Move all your logic to a separate function
  3. For db access use PHP's PDO API or an ORM like redBean
  4. Write comments in your code Comments should explain what logic has been implemented.
  5. Use better variable names.
  6. Use DateTime instead of date() (See comment below).

You want an array where the key is the full month name. You'll want to do something like this...


Here is a sample script: 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'];
}

SAMPLE OUTPUT
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"
  }
}

Instead of printing the dates as and when add the dates to an array like

wherever you are using echo $test_date apend to an array say

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

and now use the loop to display dates

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

}

I dont know the format to print the month name from the date. So please ignore print month name and replace with the actual php function to print month name


You could do something like this to get what you are looking for

Use DateTime like Dutchie432 suggested

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

Need Your Help

No more quick help in Xcode editor

xcode macos xcode5

Quick help has gone away in my Xcode 5 (5.0.2 on OS X 10.8.5):

How to use 51Degrees via NuGet with Azure?

asp.net asp.net-mvc-3 nuget 51degrees

I'm tryign to use 51Degrees in a .NET project that I deploy to Azure. August 2011, they released v1.2.1.3 marked as "Azure Compatible":