gpt4 book ai didi

php - 不同年份和月份的日期范围

转载 作者:行者123 更新时间:2023-11-30 22:36:00 26 4
gpt4 key购买 nike

你好,我有一项大学作业是关于员工假期的,我必须输出相应月份和年份的假期。

测试数据: 乔恩史密斯开始日期 28.12.2015 结束日期 2.1.2016

我可以使用 开始日期:2015 年 12 月,john Smith 28.12.2015 结束于 2.1.2016

但我还需要这些数据在

2016 January john smith 28.12.2015 ending 2.1.2016  

因为假期从 2015 年 12 月开始到 2016 年 1 月结束,

$sql = "SELECT DATE_FORMAT(datestart, '%M') as month,
DATE_FORMAT(dateEnd, '%M') as monthe,
MONTH(datestart) AS m,
MONTH(dateEnd) AS e,YEAR(datestart) AS year,
holiday.Id, employeeId, datestart,dateEnd,
DATE_FORMAT(holiday.dateEnd,'%D %M, %Y') AS end,
DATE_FORMAT(holiday.datestart,'%D %M, %Y') AS startit,
CONCAT(employees.empFirst,' ',employees.empLast) AS employee,
DATE_FORMAT(holiday.datestart,'%d-%m-%Y %H:%i') AS start From holiday
LEFT JOIN employees ON holiday.employeeId = employees.empId ORDER BY datestart ";

$result = $mysqli->query($sql);
$result1 = $mysqli->query($sql);
$year = array();
$monthx = array('January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October',
'November', 'December');

while ($row = mysqli_fetch_assoc($result)) {
$year = $row['year'];
$month = $row['month'];
$monthe = $row['monthe'];
$years[$year][$month][] = $row;
//print_r($something);
}

foreach($years as $year => $months) {
echo '<b>Year &nbsp;'.$year. '</b><br>';

foreach ($monthx as $month) {
echo '<b>'.$month. '</b><br>';
if (isset($months[$month]))
{
foreach($months[$month] as $item) {
echo $item['employee']. '&nbsp;-&nbsp;' .$item['startit'].'&nbsp;-&nbsp;'.$item['end'].'<br/>';
}
}else{
echo 'No Holidays Booked<br/>';
}
}
}

我如何合并结束日期,以便假期分散在例如。开始日期 28.12.2015 结束日期 2.1.2016 将在每个所需的年份和月份中。

最佳答案

我不确定是否有很多关于数据库中日期处理的清晰简单的文章。这里有 Snodgrass 教授的精彩著作的 PDF 版本。这很清楚,但并不简单。 http://www.cs.arizona.edu/~rts/publications.html

您有两个日期范围:a:astart - aend 和 b:bstart - bend。

如果你想知道b是否是a的真子集,逻辑是这样的:

 bstart >= astart   AND   bend <= aend

如果想知道b和a是不是完全不重叠,逻辑是这样的

bstart > aend  OR astart > bend

然后,您只需要良好的旧 SQL 日期算法....

 WHERE holiday.start >= person.start /* holiday subset of person dates */ 
AND holiday.start <= person.end

并且,如果该人的范围与假日范围重叠,

 WHERE NOT (holiday.start > person.end OR person.start > holiday.end)

关于php - 不同年份和月份的日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32553210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com