我想计算db中存储的当前时间和时间之间的时差。 d/M/y H:i:s A
是MS Sql存储日期和时间的格式。
以下代码返回错误的值。
function time_difference($created_time)
{
$str = $created_time; // 03/Mar/17 12:35:00 PM
$today = date('d/M/y H:i:s A'); // 06/Mar/17 06:35:15 AM
$time_difference = $today - $str; // Returns 3
}
无论如何都要将$str
和$today
转换为秒?
答案 0 :(得分:3)
如下所示: -
<?php
function time_difference($created_time)
{
$str = $created_time; // 03/Mar/17 12:35:00 PM
$today = date('d/M/y H:i:s A'); // 06/Mar/17 06:35:15 AM
return $time_difference = strtotime(str_replace('/','-',$today)) - strtotime(str_replace('/','-',$str));
}
echo time_difference("03/Mar/17 12:35:00 PM");
输出: - https://eval.in/748622
答案 1 :(得分:1)
function time_difference($created_time)
{
$created = DateTime::createFromFormat('d/M/y H:i:s A', $created_time);
return strtotime('now') - $created->getTimestamp();
}
答案 2 :(得分:1)
<?php
function time_difference($created_time, $date ='',$new_date ='',$time_diff ='')
{
$date = DateTime::createFromFormat('d/M/y H:i:s A', $created_time);
$new_date = $date->format('Y-m-d h:i:s');
$time_diff = strtotime(date('Y-m-d h:i:s')) - strtotime($new_date);
return $time_diff;
}
echo time_difference('03/Mar/17 12:35:00 PM');
?>