如何在PHP中计算两个日期之间的天数?
如果我有两个字符串如何在PHP中计算两个日期之间的天数?,php,date,count,Php,Date,Count,如果我有两个字符串$startDate和$endDate,它们被设置为(例如)“2011/07/01”和“2011/07/17”(表示2011年7月1日和2011年7月17日)。如何计算从开始日期到结束日期的天数?在给出的示例中,这将是17天。PHP有一个date_diff()函数来完成此操作。(也称为date_diff): 或: 然后,您可以通过调用$interval->days以整数形式获取间隔。下面是实现此操作的原始方法 $startTimeStamp = strtotime("2011/
$startDate
和$endDate
,它们被设置为(例如)“2011/07/01”
和“2011/07/17”
(表示2011年7月1日和2011年7月17日)。如何计算从开始日期到结束日期的天数?在给出的示例中,这将是17天。PHP有一个date_diff()函数来完成此操作。(也称为date_diff
):
或:
然后,您可以通过调用
$interval->days
以整数形式获取间隔。下面是实现此操作的原始方法
$startTimeStamp = strtotime("2011/07/01");
$endTimeStamp = strtotime("2011/07/17");
$timeDiff = abs($endTimeStamp - $startTimeStamp);
$numberDays = $timeDiff/86400; // 86400 seconds in one day
// and you might want to convert to integer
$numberDays = intval($numberDays);
没有一个解决方案对我有效。对于那些仍然使用PHP5.2(DateTime::diff在5.3中引入)的用户,此解决方案可以:
function howDays($from, $to) {
$first_date = strtotime($from);
$second_date = strtotime($to);
$offset = $second_date-$first_date;
return floor($offset/60/60/24);
}
来源:如果您的日期时间也有小时:分钟:秒,并且您仍然希望有天数
/**
* Returns the total number of days between to DateTimes,
* if it is within the same year
* @param $start
* @param $end
*/
public function dateTimesToDays($start,$end){
return intval($end->format('z')) - intval($start->format('z')) + 1;
}
如果您想知道天数(如果有)、小时数(如果有)、分钟数(如果有)和秒数,可以执行以下操作:
$previousTimeStamp = strtotime("2011/07/01 21:12:34");
$lastTimeStamp = strtotime("2013/09/17 12:34:11");
$menos=$lastTimeStamp-$previousTimeStamp;
$mins=$menos/60;
if($mins<1){
$showing= $menos . " seconds ago";
}
else{
$minsfinal=floor($mins);
$secondsfinal=$menos-($minsfinal*60);
$hours=$minsfinal/60;
if($hours<1){
$showing= $minsfinal . " minutes and " . $secondsfinal. " seconds ago";
}
else{
$hoursfinal=floor($hours);
$minssuperfinal=$minsfinal-($hoursfinal*60);
$days=$hoursfinal/24;
if($days<1){
$showing= $hoursfinal . "hours, " . $minssuperfinal . " minutes and " . $secondsfinal. " seconds ago";
}
else{
$daysfinal=floor($days);
$hourssuperfinal=$hoursfinal-($daysfinal*24);
$showing= $daysfinal. "days, " .$hourssuperfinal . " hours, " . $minssuperfinal . " minutes and " . $secondsfinal. " seconds ago";
}}}
echo $showing;
$previousTimeStamp=strotime(“2011/07/01 21:12:34”);
$lastTimeStamp=STROTIME(“2013/09/17 12:34:11”);
$menos=$lastTimeStamp-$previousTimeStamp;
$mins=$menos/60;
如果($mins简单的计数方法是
$currentdate = date('Y-m-d H:i:s');
$after1yrdate = date("Y-m-d H:i:s", strtotime("+1 year", strtotime($data)));
$diff = (strtotime($after1yrdate) - strtotime($currentdate)) / (60 * 60 * 24);
echo "<p style='color:red'>The difference is ".round($diff)." Days</p>";
$currentdate=date('Y-m-dh:i:s');
$after1yrdate=日期(“Y-m-d H:i:s”,标准时间(“+1年”,标准时间($data));
$diff=(strotime($after1yrdate)-strotime($currentdate))/(60*60*24);
echo“差异是“.round($diff)。“Days
”;
您可以使用日期_diff
计算两个日期之间的差值:
$date1 = date_create("2013-03-15");
$date2 = date_create("2013-12-12");
$diff = date_diff($date1 , $date2);
echo $diff->format("%R%a days");
我创建了一个函数,如果您传递两个日期,它将返回按天计算的值。为了更好地理解,请参阅开始日期的输出:2018-11-12 11:41:19和结束日期2018-11-16 12:07:26
private function getTimeData($str1,$str2){
$datetime1 = strtotime($str1);
$datetime2 = strtotime($str2);
$myArray = array();
if(date('d', $datetime2) != date('d', $datetime1) || date('m', $datetime2) != date('m', $datetime1) || date('y', $datetime2) != date('y', $datetime1)){
$exStr1 = explode(' ',$str1);
$exStr2 = explode(' ',$str2);
$datediff = strtotime($exStr2[0]) - strtotime($exStr1[0]);
$totalDays = round($datediff / (60 * 60 * 24));
$actualDate1 = $datetime1;
$actualDate2 = date('Y-m-d', $datetime1)." 23:59:59";
$interval = abs(strtotime($actualDate2)-$actualDate1);
$minutes = round($interval / 60);
$myArray[0]['startDate'] = date('Y-m-d H:i:s', $actualDate1);
$myArray[0]['endDate'] = $actualDate2;
$myArray[0]['minutes'] = $minutes;
$i = 1;
if($totalDays > 1){
for($i=1; $i<$totalDays; $i++){
$dayString = "+".$i." day";
$edate = strtotime($dayString, $actualDate1);
$myArray[$i]['startDate'] = date('Y-m-d', $edate)." 00:00:00";
$myArray[$i]['endDate'] = date('Y-m-d', $edate)." 23:59:59";
$myArray[$i]['minutes'] = 1440;
}
}
$actualSecDate1 = date('Y-m-d', $datetime2)." 00:00:00";
$actualSecDate2 = $datetime2;
$interval = abs(strtotime($actualSecDate1)-$actualSecDate2);
$minutes = round($interval / 60);
$myArray[$i]['startDate'] = $actualSecDate1;
$myArray[$i]['endDate'] = date('Y-m-d H:i:s', $actualSecDate2);
$myArray[$i]['minutes'] = $minutes;
}
else{
$interval = abs($datetime2-$datetime1);
$minutes = round($interval / 60);
$myArray[0]['startDate'] = date('Y-m-d H:i:s', $datetime1);
$myArray[0]['endDate'] = date('Y-m-d H:i:s', $datetime2);
$myArray[0]['minutes'] = $minutes;
}
return $myArray;
}
当您的本地日期包含dayling储蓄时间切换时,将无法正常工作,因为在这些日期中,您有23或25小时的天数。实际上,这些日期显示16天。我是否需要让日期忽略夏令时?应该是16天,因为7月1日和7月17日相隔16天,而7月1日和7月18日相隔16天17天apart@MichaelBorgwardt你有两个固定的日期,我看不出夏令时会有什么影响。@Andy:如果你说的“固定”是指“文字”,当然,但问题是“例如”,而不是“我需要2011年这些特定日期的解决方案,不关心其他日期可能出现的任何问题”。这似乎不适用于我的初始字符串“2011/07/01”和“2011/07/17”…“调用未定义的函数date_diff()”。根据文档,我使用的是PHP 5.2.10(链接在顶部),这是PHP>=5.3.0。下次,请在您的问题中包含PHP版本。仅当您的日期时间中没有时间时才有效,否则它可能会返回fewbug 1-2天:$interval->days return 6015天!如果我使用$interval->格式(“%d”),它只返回天数之间的差异,不考虑月份和年份。请注意,如果要在计数中同时包含开始日和结束日,则需要在结果中添加1。请参阅
$currentdate = date('Y-m-d H:i:s');
$after1yrdate = date("Y-m-d H:i:s", strtotime("+1 year", strtotime($data)));
$diff = (strtotime($after1yrdate) - strtotime($currentdate)) / (60 * 60 * 24);
echo "<p style='color:red'>The difference is ".round($diff)." Days</p>";
$date1 = date_create("2013-03-15");
$date2 = date_create("2013-12-12");
$diff = date_diff($date1 , $date2);
echo $diff->format("%R%a days");
$date1 = date_create("2017-04-15");
$date2 = date_create("2017-05-18");
//difference between two dates
$diff = date_diff($date1,$date2);
//count days
echo 'Days Count - '.$diff->format("%a");
private function getTimeData($str1,$str2){
$datetime1 = strtotime($str1);
$datetime2 = strtotime($str2);
$myArray = array();
if(date('d', $datetime2) != date('d', $datetime1) || date('m', $datetime2) != date('m', $datetime1) || date('y', $datetime2) != date('y', $datetime1)){
$exStr1 = explode(' ',$str1);
$exStr2 = explode(' ',$str2);
$datediff = strtotime($exStr2[0]) - strtotime($exStr1[0]);
$totalDays = round($datediff / (60 * 60 * 24));
$actualDate1 = $datetime1;
$actualDate2 = date('Y-m-d', $datetime1)." 23:59:59";
$interval = abs(strtotime($actualDate2)-$actualDate1);
$minutes = round($interval / 60);
$myArray[0]['startDate'] = date('Y-m-d H:i:s', $actualDate1);
$myArray[0]['endDate'] = $actualDate2;
$myArray[0]['minutes'] = $minutes;
$i = 1;
if($totalDays > 1){
for($i=1; $i<$totalDays; $i++){
$dayString = "+".$i." day";
$edate = strtotime($dayString, $actualDate1);
$myArray[$i]['startDate'] = date('Y-m-d', $edate)." 00:00:00";
$myArray[$i]['endDate'] = date('Y-m-d', $edate)." 23:59:59";
$myArray[$i]['minutes'] = 1440;
}
}
$actualSecDate1 = date('Y-m-d', $datetime2)." 00:00:00";
$actualSecDate2 = $datetime2;
$interval = abs(strtotime($actualSecDate1)-$actualSecDate2);
$minutes = round($interval / 60);
$myArray[$i]['startDate'] = $actualSecDate1;
$myArray[$i]['endDate'] = date('Y-m-d H:i:s', $actualSecDate2);
$myArray[$i]['minutes'] = $minutes;
}
else{
$interval = abs($datetime2-$datetime1);
$minutes = round($interval / 60);
$myArray[0]['startDate'] = date('Y-m-d H:i:s', $datetime1);
$myArray[0]['endDate'] = date('Y-m-d H:i:s', $datetime2);
$myArray[0]['minutes'] = $minutes;
}
return $myArray;
}
Array
(
[0] => Array
(
[startDate] => 2018-11-12 11:41:19
[endDate] => 2018-11-12 23:59:59
[minutes] => 739
)
[1] => Array
(
[startDate] => 2018-11-13 00:00:00
[endDate] => 2018-11-13 23:59:59
[minutes] => 1440
)
[2] => Array
(
[startDate] => 2018-11-14 00:00:00
[endDate] => 2018-11-14 23:59:59
[minutes] => 1440
)
[3] => Array
(
[startDate] => 2018-11-15 00:00:00
[endDate] => 2018-11-15 23:59:59
[minutes] => 1440
)
[4] => Array
(
[startDate] => 2018-11-16 00:00:00
[endDate] => 2018-11-16 12:07:26
[minutes] => 727
)
)