如何在PHP中计算两个日期时间之间的分钟差异?
当前回答
我想这对你有帮助
function calculate_time_span($date){
$seconds = strtotime(date('Y-m-d H:i:s')) - strtotime($date);
$months = floor($seconds / (3600*24*30));
$day = floor($seconds / (3600*24));
$hours = floor($seconds / 3600);
$mins = floor(($seconds - ($hours*3600)) / 60);
$secs = floor($seconds % 60);
if($seconds < 60)
$time = $secs." seconds ago";
else if($seconds < 60*60 )
$time = $mins." min ago";
else if($seconds < 24*60*60)
$time = $hours." hours ago";
else if($seconds < 24*60*60)
$time = $day." day ago";
else
$time = $months." month ago";
return $time;
}
其他回答
DateTime::diff很酷,但对于这种需要单个单元结果的计算来说很尴尬。手动减去时间戳效果更好:
$date1 = new DateTime('2020-09-01 01:00:00');
$date2 = new DateTime('2021-09-01 14:00:00');
$diff_mins = abs($date1->getTimestamp() - $date2->getTimestamp()) / 60;
求两个日期之差的方法在这里。有了这个功能,你可以找到秒、分、小时、天、年和月等差异。
function alihan_diff_dates($date = null, $diff = "minutes") {
$start_date = new DateTime($date);
$since_start = $start_date->diff(new DateTime( date('Y-m-d H:i:s') )); // date now
print_r($since_start);
switch ($diff) {
case 'seconds':
return $since_start->s;
break;
case 'minutes':
return $since_start->i;
break;
case 'hours':
return $since_start->h;
break;
case 'days':
return $since_start->d;
break;
default:
# code...
break;
}
}
你可以开发这个函数。我测试过了,结果对我有用。DateInterval对象输出如下:
/*
DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 0 [i] => 5 [s] => 13 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
*/
功能用途:
$date =过去的日期, $diff =类型eg:“分钟”,“天”,“秒”
$diff_mins = alihan_diff_dates("2019-03-24 13:24:19", "minutes");
祝你好运。
这将有助于....
function get_time($date,$nosuffix=''){
$datetime = new DateTime($date);
$interval = date_create('now')->diff( $datetime );
if(empty($nosuffix))$suffix = ( $interval->invert ? ' ago' : '' );
else $suffix='';
//return $interval->y;
if($interval->y >=1) {$count = date(VDATE, strtotime($date)); $text = '';}
elseif($interval->m >=1) {$count = date('M d', strtotime($date)); $text = '';}
elseif($interval->d >=1) {$count = $interval->d; $text = 'day';}
elseif($interval->h >=1) {$count = $interval->h; $text = 'hour';}
elseif($interval->i >=1) {$count = $interval->i; $text = 'minute';}
elseif($interval->s ==0) {$count = 'Just Now'; $text = '';}
else {$count = $interval->s; $text = 'second';}
if(empty($text)) return '<i class="fa fa-clock-o"></i> '.$count;
return '<i class="fa fa-clock-o"></i> '.$count.(($count ==1)?(" $text"):(" ${text}s")).' '.$suffix;
}
试试这个
$now = \Carbon\Carbon::now()->toDateString(); // get current time
$a = strtotime("2012-09-21 12:12:22");
$b = strtotime($now);
$minutes = ceil(($a - $b) / 3600); it will get ceiling value
我想这对你有帮助
function calculate_time_span($date){
$seconds = strtotime(date('Y-m-d H:i:s')) - strtotime($date);
$months = floor($seconds / (3600*24*30));
$day = floor($seconds / (3600*24));
$hours = floor($seconds / 3600);
$mins = floor(($seconds - ($hours*3600)) / 60);
$secs = floor($seconds % 60);
if($seconds < 60)
$time = $secs." seconds ago";
else if($seconds < 60*60 )
$time = $mins." min ago";
else if($seconds < 24*60*60)
$time = $hours." hours ago";
else if($seconds < 24*60*60)
$time = $day." day ago";
else
$time = $months." month ago";
return $time;
}
推荐文章
- 致命错误:未找到类“SoapClient”
- 我目前使用的是哪个版本的CodeIgniter ?
- 合并两个PHP对象的最佳方法是什么?
- 我如何预填充一个jQuery Datepicker文本框与今天的日期?
- 如何使HTTP请求在PHP和不等待响应
- 发送附件与PHP邮件()?
- 如何获得当前的路线在Symfony 2?
- 用PHP删除字符串的前4个字符
- 当js得到当月的第一天和最后一天
- mysql_connect():[2002]没有这样的文件或目录(试图通过unix:///tmp/mysql.sock连接)在
- 一个函数的多个返回值
- 在PHP中使用foreach循环时查找数组的最后一个元素
- 如何删除列表中的最后一项?
- 检查数组是否为空
- PHP DOMDocument loadHTML没有正确编码UTF-8