在我的模特中
$this->db->select('message,TIMESTAMPDIFF(MINUTE,date_added,CURRENT_TIMESTAMP) AS minutes_passed');
$this->db->from('activity_logs');
$this->db->order_by('log_id','desc');
$this->db->limit(20);
$q = $this->db->get();
return $q->result_array();
控制器中的
$data['logs'] = $this->common_model->get_logs();
foreach ($data['logs'] as $k) {
echo 'total minutes'.$k['minutes_passed']."<br>";//output total minutes "21410"
}
我有总分钟数,我希望得到与分钟相关的总小时数,然后将小时数转换为总天数。 我希望有人能指导我完成这件事。 感谢。
答案 0 :(得分:2)
尝试此功能,
&安培;根据您的要求修改
function con_min_days($mins)
{
$hours = str_pad(floor($mins /60),2,"0",STR_PAD_LEFT);
$mins = str_pad($mins %60,2,"0",STR_PAD_LEFT);
if((int)$hours > 24){
$days = str_pad(floor($hours /24),2,"0",STR_PAD_LEFT);
$hours = str_pad($hours %24,2,"0",STR_PAD_LEFT);
}
if(isset($days)) { $days = $days." Day[s] ";}
return $days.$hours." Hour[s] ".$mins." Min[s]";
}
答案 1 :(得分:0)
我认为你应该只是从你的数据库中获取日期,并在php中完成整个过程:
$now = new DateTime;
$date_added = new DateTime;
$date_added->setTimestamp(date_added);
$diff = $now->diff($date_added);
然后你可以打印:
sprintf("Temps écoulé : %d jours, %d minutes et %d seconds"; $diff->d, $diff->h, $diff->s);
PS:您不应该在数据库中存储时间戳,而应使用DATE或DATETIME字段类型存储日期。
答案 2 :(得分:0)
从UNIX_TIMESTAMP(date_added) as create_date
添加此功能
private function time_elapsed_string($ptime)
{
$etime = time() - $ptime;
if ($etime < 1){
return '0 seconds';
}
$a = array( 365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
$a_plural = array( 'year' => 'years',
'month' => 'months',
'day' => 'days',
'hour' => 'hours',
'minute' => 'minutes',
'second' => 'seconds'
);
foreach ($a as $secs => $str){
$d = $etime / $secs;
if ($d >= 1){
$r = round($d);
return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
}
}
}
并像这样使用它:
echo $this->time_elapsed_string($item->create_date)
O / P:
2 hour ago
5 days ago
取决于您在函数中传递的毫秒时间