Reputation: 137
How to get data in different section like today, upcoming, and overdue date with the help of foreach loop.
foreach($list as $index=>$row) {
$dueDt = new DateTime($row->due_timeDate);
$todayDt = new DateTime('now');
$due = $dueDt->format('Y-m-d');
$today = $todayDt->format('Y-m-d');
if($due == $today) {
echo $due.' Today' ;
echo '<br>';
$task['today'][]=$row;
} elseif($due>$today) {
echo $due.' Upcoming';
echo '<br>';
$task['today'][]=$row;
} elseif($due<$today) {
echo $due.' Overdue';;
echo '<br>';
$task['overdue'][]=$row;
}
}
My data is coming like this:
2015-10-30 Upcoming
2015-10-29 Today
2015-10-28 Overdue
2015-10-28 Overdue
2015-10-27 Overdue
2015-10-27 Overdue
2015-10-15 Overdue
But I want the data like this from foreach loop:
Upcoming
2015-10-30
Today
2015-10-29
Overdue
2015-10-28
2015-10-28
2015-10-27
2015-10-27
2015-10-15
Upvotes: 3
Views: 232
Reputation: 5264
According to your data-structure:
$data = ['2015-10-25','2015-10-26','2015-10-27','2015-10-28','2015-10-29','2015-10-30'];
$current = '2015-10-27';
// Saving Array Keys for future work
$rows = array('overdue','today','upcomming');
foreach($data as $date) {
// Compare each date to find our $rows array
$curt = strtotime($current);
$d = strtotime($date);
if($d < $curt) {
$rows['overdue'][] = $date;
} else if($d > $curt) {
$rows['upcomming'][] = $date;
} else if($d == $curt) {
$rows['today'][] = $date;
}
}
//Now Print each $rows:
echo 'Overdue: <br>'. implode('<br>', $rows['overdue']) .'<br><br>';
echo 'Today: <br>'. implode('<br>', $rows['today']) .'<br><br>';
echo 'Upcomming: <br>'. implode('<br>', $rows['upcomming']);
Prints:
Overdue:
2015-10-25
2015-10-26
Today:
2015-10-27
Upcomming:
2015-10-28
2015-10-29
2015-10-30
Upvotes: 0
Reputation: 38584
Try this
foreach($list as $index=>$row) {
$dueDt = new DateTime($row->due_timeDate);
$todayDt = new DateTime('now');
$due=$dueDt->format('Y-m-d');
$today=$todayDt->format('Y-m-d');
if($due==$today){
$up['upcome']=$row;
}elseif($due>$today){
$to['Today']=$row;
}elseif($due<$today){
$over['overdue']=$row;
}
if (!empty($up)) {
echo "Upcoming";
foreach ($up as $new_up) {
echo " " . $new_up['upcome']."<br>";
}
}
if (!empty($to)) {
echo "Today";
foreach ($to as $new_to) {
echo " " . $new_to['Today']."<br>";
}
}
if (!empty($over)) {
echo "Overdue";
foreach ($over as $new_over) {
echo " " . $new_over['overdue']."<br>";
}
}
}
Output will be
Upcoming
2015-10-30
Today
2015-10-29
Overdue
2015-10-28
2015-10-28
2015-10-27
2015-10-27
2015-10-15
Upvotes: 0
Reputation: 23948
You need to use key value pairs of your array.
Take key as time (Today, Tomorrow, Overdue).
This should be a multi-dimensional array.
Print in loop like following:
<?php
$tasks = array();
foreach($list as $index=>$row) {
$dueDt = new DateTime($row->due_timeDate);
$todayDt = new DateTime('now');
$due=$dueDt->format('Y-m-d');
$today=$todayDt->format('Y-m-d');
if($due==$today) {
$tasks['today'][] = $due;
}
else if($due>$today) {
$tasks['upcoming'][]=$due;
}
else if($due<$today) {
$tasks['overdue'][]=$due;
}
}
if (! empty($tasks)) {
foreach ($tasks as $dueDate => $task) {
echo ucwords($dueDate);
if (! empty($task)) {
foreach ($task as $title) {
echo "<br/>--> " . $title;;
}
}
}
}
?>
Upvotes: 1
Reputation: 3427
$task['today'] = array();
$task['Upcoming'] = array();
$task['Overdue'] = array();
foreach ($list as $index => $row)
{
$dueDt = new DateTime($row->due_timeDate);
$todayDt = new DateTime('now');
$due = $dueDt->format('Y-m-d');
$today = $todayDt->format('Y-m-d');
if ($due == $today)
{
$task['today'][] = $row;
}
elseif ($due > $today)
{
$task['Upcoming'][] = $row;
}
elseif ($due < $today)
{
$task['Overdue'][] = $row;
}
}
foreach ($tasks as $dueDate => $task)
{
if (!empty($task))
{
echo $dueDate . "<br />";
foreach ($task as $date)
{
echo " " . $date . "<br />";
}
}
}
Upvotes: 3