Reputation: 51
I'm trying to make a table of years and months from date to date but I can't make it work like I want.
NOTE: dates will be dynamically changed in my app.
See the images below.
This is my code:
<?php
$start = (new DateTime('2020-08-02'))->modify('first day of this month');
$end = (new DateTime('2022-05-06'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
?>
<table border="1">
<tr>
<?php
foreach ($period as $dt) {
echo "<td>" . $dt->format("Y") . "</td>";
}
?>
</tr>
<tr>
<?php
foreach ($period as $dt) {
echo "<td>" . $dt->format("m") . "</td>";
}
?>
</tr>
</table>
UPDATED: Now years and months works great, I have also added weeks to this table, but I have a problem with merging year row.
See the example how it looks like now
this is updated code:
<?php
include "db.php";
$date1 = '2020-11-02';
$date2 = '2021-03-06';
$start = (new DateTime($date1))->modify('first day of this month');
$end = (new DateTime($date2))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
?>
<table border="1">
<tr>
<?php
$d1 = date_parse_from_format("Y-m-d", $date1);
$d2 = date_parse_from_format("Y-m-d", $date2);
$f_val = 13 - $d1['month'];
$s_val = $d2['month'];
$ar = array();
$ar1 = array();
$th_span = array($f_val, 12, $s_val);
$i = 0;
foreach ($period as $dt) {
$ar += array($dt->format("Y") => 0);
$ar1 += array($dt->format("m") => 0);
}
foreach ($ar as $k => $v) {
$number = 0;
foreach ($ar1 as $k1 => $v1) {
$q = $con->prepare("SELECT COUNT(id) AS total FROM nedelje WHERE godina=? AND mesec=?");
$q->bind_param("ii", $k, $k1);
$q->execute();
$r = $q->get_result();
$v = $r->fetch_assoc();
$num = $v['total'];
$number = $number + $num;
}
$num1 = $th_span[$i];
$number1 = (int)$num1 * $number;
echo "<th colspan=" . $number1 . ">" . $k . "</th>";
$i++;
}
?>
</tr>
<tr>
<?php
foreach ($period as $dt) {
$month = $dt->format("m");
$year = $dt->format("Y");
$q = $con->prepare("SELECT COUNT(id) AS total FROM nedelje WHERE godina=? AND mesec=?");
$q->bind_param("ii", $year, $month);
$q->execute();
$r = $q->get_result();
$v = $r->fetch_assoc();
$number = $v['total'];
echo "<td colspan=" . $number . ">" . $dt->format("F") . "</td>";
}
?>
</tr>
<tr>
<?php
foreach ($period as $dt) {
$mesec = $dt->format("m");
$godina = $dt->format("Y");
$sql = $con->prepare("SELECT nedelja FROM nedelje WHERE godina=? AND mesec=?");
$sql->bind_param("ii", $godina, $mesec);
$sql->execute();
$res = $sql->get_result();
while ($row = $res->fetch_assoc()) {
echo "<td>Week " . $row['nedelja'] . "</td>";
}
}
?>
</tr>
</table>
Upvotes: 1
Views: 452
Reputation: 135
You can use this code:
function addMonth(strDate, strDateEnd, count)
{
var d = new Date(strDate);
if(d == 'Invalid Date') return 'BAD DATE!';
d = new Date(strDateEnd);
if(d == 'Invalid Date') return 'BAD END DATE!';
const param = strDate.trim().split('-');
var year = parseInt(param[0]);
var month = parseInt(param[1]);
var day = parseInt(param[2]);
const param1= strDate.trim().split('-');
var e_year = parseInt(param1[0]);
var e_month = parseInt(param1[1]);
var e_day = parseInt(param1[2]);
for(var c=0; c<count; c++)
{
month++;
if(month == 13)
{
month = 1;
year++;
}
strDate = year + "-" + month + "-" + day;
if(e_year < year) break;
if(e_year == year && e_month > month) break;
if(e_year == year && e_month == month && e_day > day) break;
}
var is29D = false;
if( year % 4 === 0 ) is29D = true;
if( year % 100 === 0 && year % 400 === 0 ) { is29D = true; }
if(month == 2 && day > 28 && !is29D) { day = 28; }
if(month == 2 && day > 29 && is29D ) { day = 29; }
if( day > 30 && ( month == 4 || month == 6 || month == 9 || month == 11 ) ) { day = 30; }
if(month < 10) month = '0'+month;
if(day < 10) day = '0'+day;
return year + "-" + month + "-" + day;
}
Upvotes: 0
Reputation: 179
Maybe there is another simple way. But this one is working fine for me. To eliminate duplicate values, I created a new array called $ar and stored all year values as key in it. This will help to eliminate all duplicate values.
<?php
$date1 = '2020-08-02';
$date2 ='2022-05-06';
$start = (new DateTime($date1))->modify('first day of this month');
$end = (new DateTime($date2))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
?>
<table border="1">
<tr>
<?php
$d1 = date_parse_from_format("Y-m-d",$date1);
$d2 = date_parse_from_format("Y-m-d",$date2);
$f_val = 13-$d1['month'];
$s_val = $d2['month'];
$ar = array();
$th_span = array($f_val,12,$s_val);
$i = 0;
foreach ($period as $dt) {
$ar += array($dt->format("Y") => 0);
}
foreach($ar as $k => $v) {
echo "<th colspan=".$th_span[$i].">" . $k . "</th>";
$i++;
}
?>
</tr>
<tr>
<?php
foreach ($period as $dt) {
echo "<td>" . $dt->format("F") . "</td>";
}
?>
</tr>
</table>
Upvotes: 1