Php 计算两个日期之间的天数
我想做的是计算两个日期之间的天数,不包括周末和我;我已经用下面的函数完成了。但是每当Php 计算两个日期之间的天数,php,function,Php,Function,我想做的是计算两个日期之间的天数,不包括周末和我;我已经用下面的函数完成了。但是每当$startDate大于$endDate时,我就无法得到正确的结果。我试着使用if($startDate>$endDate),我对这种情况很熟悉,真的不知道下一步该怎么办 function getWorkingDays($startDate,$endDate){ // do strtotime calculations just once $startDate = strtotim
$startDate
大于$endDate
时,我就无法得到正确的结果。我试着使用if($startDate>$endDate)
,我对这种情况很熟悉,真的不知道下一步该怎么办
function getWorkingDays($startDate,$endDate){
// do strtotime calculations just once
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
//The total number of days between the two dates. We compute the no. of seconds and divide it to 60*60*24
//We add one to inlude both dates in the interval.
$days = ($endDate - $startDate) / 86400 + 0;
$no_full_weeks = floor($days / 7);
$no_remaining_days = fmod($days, 7);
//It will return 1 if it's Monday,.. ,7 for Sunday
$the_first_day_of_week = date("N", $startDate);
$the_last_day_of_week = date("N", $endDate);
// If one of the value is empty it will return "0"
if ($startDate == '' || $endDate == '')
return "0"; // Default value
//---->The two can be equal in leap years when february has 29 days, the equal sign is added here
//In the first case the whole interval is within a week, in the second case the interval falls in two weeks.
if ($the_first_day_of_week <= $the_last_day_of_week) {
if ($the_first_day_of_week <= 6 && 6 <= $the_last_day_of_week) $no_remaining_days--;
if ($the_first_day_of_week <= 7 && 7 <= $the_last_day_of_week) $no_remaining_days--;
}
else {
// (edit by Tokes to fix an edge case where the start day was a Sunday
// and the end day was NOT a Saturday)
// the day of the week for start is later than the day of the week for end
if ($the_first_day_of_week == 7) {
// if the start date is a Sunday, then we definitely subtract 1 day
$no_remaining_days--;
if ($the_last_day_of_week == 6) {
// if the end date is a Saturday, then we subtract another day
$no_remaining_days--;
}
}
else {
// the start date was a Saturday (or earlier), and the end date was (Mon..Fri)
// so we skip an entire weekend and subtract 2 days
$no_remaining_days -= 2;
}
}
//The no. of business days is: (number of weeks between the two dates) * (5 working days) + the remainder
//---->february in none leap years gave a remainder of 0 but still calculated weekends between first and last day, this is one way to fix it
$workingDays = $no_full_weeks * 5;
if ($no_remaining_days > 0 )
{
$workingDays += $no_remaining_days;
}
return $workingDays;
}
函数getWorkingDays($startDate,$endDate){
//只进行一次strotime计算
$startDate=strottime($startDate);
$endDate=strottime($endDate);
//两个日期之间的总天数。我们计算秒数并将其除以60*60*24
//我们在间隔中的两个日期中都添加一个。
$days=($endDate-$startDate)/86400+0;
$no_full_weeks=最低限额($days/7);
$no_剩余天数=fmod($d,7);
//如果是星期一,它将返回1,如果是星期天,它将返回7
$每周的第一天=日期(“N”,开始日期);
$U周的最后一天=日期(“N”,$endDate);
//如果其中一个值为空,它将返回“0”
如果($startDate=''| |$endDate='')
返回“0”;//默认值
//---->二者在闰年中可以相等,二月有29天,这里加上等号
//在第一种情况下,整个间隔在一周内,在第二种情况下,间隔在两周内。
如果($每周的第一天使用date\u diff()
,它返回两个DateTime对象之间的差值
$diff=date_diff($startDate,$endDate);
使用date\u diff()
返回两个DateTime对象之间的差异
$diff=date_diff($startDate,$endDate);
有这样做的代码脚本
<?php
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;
echo floor($datediff / (60 * 60 * 24));
?>
有这样做的代码脚本
<?php
$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;
echo floor($datediff / (60 * 60 * 24));
?>
或
对于计数天数(不包括使用以下代码)
$start = new DateTime('7/17/2017');
$end = new DateTime('7/24/2017');
$oneday = new DateInterval("P1D");
$daysName = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$days = array();
foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
$day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
if($day_num < 6) { /* weekday */
$days[$day->format("Y-m-d")] = date('D', strtotime($day->format("Y-m-d")));;
}
}
echo "<pre>";
print_r($days);
echo count($days);
$start=新日期时间('7/17/2017');
$end=新日期时间(“2017年7月24日”);
$oneday=新的日期间隔(“P1D”);
$daysName=数组('Mon','Tue','Wed','Thu','Fri');
$days=array();
foreach(新日期周期($start、$oneday、$end->add($oneday))作为$day){
$day_num=$day->format(“N”);/*'N'第1天(周一)到第7天(周日)*/
如果($day_num<6){/*工作日*/
$days[$day->format(“Y-m-d”)]=日期(“d”,标准时间($day->format(“Y-m-d”));;
}
}
回声“;
打印(天);
回声计数(天);
或
对于计数天数(不包括使用以下代码)
$start = new DateTime('7/17/2017');
$end = new DateTime('7/24/2017');
$oneday = new DateInterval("P1D");
$daysName = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
$days = array();
foreach(new DatePeriod($start, $oneday, $end->add($oneday)) as $day) {
$day_num = $day->format("N"); /* 'N' number days 1 (mon) to 7 (sun) */
if($day_num < 6) { /* weekday */
$days[$day->format("Y-m-d")] = date('D', strtotime($day->format("Y-m-d")));;
}
}
echo "<pre>";
print_r($days);
echo count($days);
$start=新日期时间('7/17/2017');
$end=新日期时间(“2017年7月24日”);
$oneday=新的日期间隔(“P1D”);
$daysName=数组('Mon','Tue','Wed','Thu','Fri');
$days=array();
foreach(新日期周期($start、$oneday、$end->add($oneday))作为$day){
$day_num=$day->format(“N”);/*'N'第1天(周一)到第7天(周日)*/
如果($day_num<6){/*工作日*/
$days[$day->format(“Y-m-d”)]=日期(“d”,标准时间($day->format(“Y-m-d”));;
}
}
回声“;
打印(天);
回声计数(天);
这将检查开始日期是否小于结束日期。如果是,则将显示天数
//getWorkingDays(start_date, end_date)
if($days = getWorkingDays("2017-05-01","2018-01-01")){
echo $days;
}
function getWorkingDays($startDate,$endDate){
$days = false;
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
if($startDate <= $endDate){
$datediff = $endDate - $startDate;
$days = floor($datediff / (60 * 60 * 24)); // Total Nos Of Days
$sundays = intval($days / 7) + (date('N', $startDate) + $days % 7 >= 7); // Total Nos Of Sundays Between Start Date & End Date
$saturdays = intval($days / 7) + (date('N', $startDate) + $days % 6 >= 6); // Total Nos Of Saturdays Between Start Date & End Date
$days = $days - ($sundays + $saturdays); // Total Nos Of Days Excluding Weekends
}
return $days;
}
?>
输出:245
使用的功能:
//getWorkingDays(开始日期、结束日期)
如果($days=getWorkingDays(“2017-05-01”、“2018-01-01”)){
回声$天;
}
函数getWorkingDays($startDate,$endDate){
$days=假;
$startDate=strottime($startDate);
$endDate=strottime($endDate);
if($startDate=7);//开始日期和结束日期之间的星期日总数
$saturdays=intval($days/7)+(日期('N',$startDate)+$days%6>=6);//开始日期和结束日期之间的星期六总数
$days=$days-($sundays+$saturdays);//不包括周末在内的总天数
}
返回$days;
}
?>
来源:
这将检查开始日期是否小于结束日期。如果是,则将显示天数
//getWorkingDays(start_date, end_date)
if($days = getWorkingDays("2017-05-01","2018-01-01")){
echo $days;
}
function getWorkingDays($startDate,$endDate){
$days = false;
$startDate = strtotime($startDate);
$endDate = strtotime($endDate);
if($startDate <= $endDate){
$datediff = $endDate - $startDate;
$days = floor($datediff / (60 * 60 * 24)); // Total Nos Of Days
$sundays = intval($days / 7) + (date('N', $startDate) + $days % 7 >= 7); // Total Nos Of Sundays Between Start Date & End Date
$saturdays = intval($days / 7) + (date('N', $startDate) + $days % 6 >= 6); // Total Nos Of Saturdays Between Start Date & End Date
$days = $days - ($sundays + $saturdays); // Total Nos Of Days Excluding Weekends
}
return $days;
}
?>
输出:245
使用的功能:
//getWorkingDays(开始日期、结束日期)
如果($days=getWorkingDays(“2017-05-01”、“2018-01-01”)){
回声$天;
}
函数getWorkingDays($startDate,$endDate){
$days=假;
$startDate=strottime($startDate);
$endDate=strottime($endDate);
if($startDate=7);//开始日期和结束日期之间的星期日总数
$saturdays=intval($days/7)+(日期('N',$startDate)+$days%6>=6);//开始日期和结束日期之间的星期六总数
$days=$days-($sundays+$saturdays);//不包括周末在内的总天数
}
返回$days;
}
?>
来源:
编辑:我在评论中注意到你想排除周末(但是你在帖子中没有提到!) 您可以添加要从一周中排除的天数 您可以使用和使用绝对结果选项(始终为正差)
livedemo()编辑:我在评论中注意到你想排除周末(但是你在帖子中没有提到!) 您可以添加要从一周中排除的天数 您可以使用和使用
<?php
function daysBetween2Dates($date1, $date2, $execludedDaysFromWeek = 0)
{
try{
$datetime1 = new \DateTime($date1);
$datetime2 = new \DateTime($date2);
}catch (\Exception $e){
return false;
}
$interval = $datetime1->diff($datetime2,true);
$days = $interval->format('%a');
if($execludedDaysFromWeek < 0 || $execludedDaysFromWeek > 7){
$execludedDaysFromWeek = 0 ;
}
return ceil($days * (7-$execludedDaysFromWeek) / 7);
}
// example 1 : without weekend days, start date is the first one
$days = daysBetween2Dates('2016-12-31','2017-12-31');
echo $days;
// example 2 : without weekend days, start date is the second one
$days = daysBetween2Dates('2017-12-31', '2016-12-31');
echo "<br>\n" .$days;
// example 3 : with weekend days, it returns 6 days for the week
$days = daysBetween2Dates('2017-12-31', '2017-12-24',-1);
echo "<br>\n" .$days;
exit;
365
365
6
public function datediff($sdate,$edate){
$diffformat='%a';
$date1 = date_create($sdate);
$date2 = date_create($edate);
$diff12 = date_diff($date2, $date1);
$days = $diff12->format($diffformat) + 1;}