Hi,
I created an events website , I have array contain events ('id','start_date','end_date','result_date') I created 3 HTML tables
- the first one for started events (today is greater than or equal to start date & today is smaller than or equal to end date )
- the second one for not started events (today is smaller than start and end date)
- the last one for end events (today is greater than start and end date) so I created this code
<?php
//set deafault timezone to turkey time zone
date_default_timezone_set("Europe/Istanbul");
//the function sorting function for usort
function build_sorter($key)
{
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
// today (2021-07-02)
$today = date('Y-m-d', time());
echo '<script> console.log("today is :' . date('Y-m-d') . '")</script>';
//sample data
$rows = array(
[
'id' => '1',
'date' => '2020-03-27',
'date_end' => '2020-05-02',
'date_res' => '2020-06-02'
],
[
'id' => '2',
'date' => '2020-04-27',
'date_end' => '2020-06-02',
'date_res' => '2020-08-02'
],
[
'id' => '3',
'date' => '2020-06-27',
'date_end' => '2020-07-02',
'date_res' => '2020-08-02'
],
[
'id' => '4',
'date' => '2021-01-01',
'date_end' => '2021-02-01',
'date_res' => '2021-03-01'
],
[
'id' => '5',
'date' => '2021-02-01',
'date_end' => '2021-03-01',
'date_res' => '2021-04-01'
],
[
'id' => '6',
'date' => '2021-05-01',
'date_end' => '2021-06-01',
'date_res' => '2021-08-01'
],
[
'id' => '7',
'date' => '2021-02-06',
'date_end' => '2021-01-29',
'date_res' => '2021-01-29'
],
[
'id' => '9',
'date' => '2021-04-27',
'date_end' => '2021-05-03',
'date_res' => '2021-05-03'
],
[
'id' => '10',
'date' => '2021-02-01',
'date_end' => '2021-02-06',
'date_res' => '2021-05-03'
],
[
'id' => '11',
'date' => '2021-02-02',
'date_end' => '2021-02-07',
'date_res' => '2021-05-05'
], [
'id' => '12',
'date' => '2021-03-27',
'date_end' => '2021-02-07',
'date_res' => '2021-06-02'
],
);
?>
<h1>started & didn't end</h1>
<table>
<thead>
<tr>
<td>id</td>
<td>start date</td>
<td>end date</td>
<td>res date</td>
</tr>
</thead>
<tbody>
<?php
// > means end
// < means didn't end
usort($rows, build_sorter('date_end'));
foreach ($rows as $row) {
if ($today < $row['date_end'] && $today >= $row['date']) {
// started & didn't end
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['date_end']; ?></td>
<td><?php echo $row['date_res']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<h1>didn't started & didn't end </h1>
<table>
<thead>
<tr>
<td>id</td>
<td>start date</td>
<td>end date</td>
<td>res date</td>
</tr>
</thead>
<tbody>
<?php
usort($rows, build_sorter('date_end'));
foreach ($rows as $row) {
if ($today < $row['date_end'] && $today < $row['date']) {
// didn't started & didn't end
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['date_end']; ?></td>
<td><?php echo $row['date_res']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<h1>started & end </h1>
<table>
<thead>
<tr>
<td>id</td>
<td>start date</td>
<td>end date</td>
<td>res date</td>
</tr>
</thead>
<tbody>
<?php
usort($rows, build_sorter('date_res'));
foreach ($rows as $row) {
if ($today > $row['date_end'] && $today > $row['date']) {
//started & end
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['date_end']; ?></td>
<td><?php echo $row['date_res']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
my erorr is
some dates don't come to tables , I think the reason that some event had start date equals to today and others got end date equals to today I wrote >= but no thing happened
I know it complex ,
THANKS,
and sorry for the weird english
Aucun commentaire:
Enregistrer un commentaire