gpt4 book ai didi

php - 具有相同日期的 Datediff 之和

转载 作者:行者123 更新时间:2023-11-29 17:59:01 27 4
gpt4 key购买 nike

我正在尝试总结日期差异,一切都很好,除了如果有相同日期我想添加 1,例如,如果起始日期是:01/01/2003到目前为止是 01/01/2003 那么我想添加 1 天,但它没有添加 1 天,而是仅在 01/01/03 到 01/02/2003 之间添加一天。这是计算休假的,如果有人请假一天,即01/01/2003到01/01/2003,则应计算1天

	$sql = "SELECT l.id as typeid,l.*,ud.firstname, ud.lastname,ud.email,l.leave_starting_from,l.leave_partial_from,ud.joining_date, l.typename, l.no_of_leave_days,(CASE SUM(datediff(STR_TO_DATE(lr.to_date, '%m/%d/%Y'), STR_TO_DATE(lr.from_date, '%m/%d/%Y'))) when NULL then '0' else SUM(datediff(STR_TO_DATE(lr.to_date, '%m/%d/%Y'), STR_TO_DATE(lr.from_date, '%m/%d/%Y'))) end) as days_in_betw,  lr.from_date, lr.to_date FROM pr_users u INNER JOIN pr_users_details ud on ud.userid= u.id LEFT OUTER JOIN pr_leave_type l ON ((FIND_IN_SET( u.departmentid , l.function ) >0) AND (FIND_IN_SET( ud.designation_id , l.level ) >0) and (l.gender='All' or l.gender=ud.gender)) LEFT OUTER JOIN pr_leave_request lr ON u.id = lr.userid and lr.sr_type = l.id";

特别是else部分

SUM(datediff(STR_TO_DATE(lr.to_date, '%m/%d/%Y'), STR_TO_DATE(lr.from_date, '%m/%d/%Y'))

最佳答案

我也遇到过同样的问题,希望这个功能能解决你的问题。

function days_calculation($start_date,$end_date){
$start = new DateTime($start_date);
$end = new DateTime($end_date);
// otherwise the end date is excluded (bug?)
$end->modify('+1 day');

$interval = $end->diff($start);

// total days
$days = $interval->days;

// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);

// best stored as array, so you can add more than one
$holidays = array();

foreach($period as $dt) {
$curr = $dt->format('D');

// substract if Saturday or Sunday
if ($curr == 'Sat' || $curr == 'Sun') {
$days--;
}

// (optional) for the updated question
elseif (in_array($dt->format('Y-m-d'), $holidays)) {
$days--;
}
}

if($days==1){
$days = $days." day";
} else {
$days = $days." days";

}

return $days;
}

关于php - 具有相同日期的 Datediff 之和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48560303/

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