Php 计票年龄
我正在创建一个票证系统,现在我正在尝试回显票证的年代。 票证日期/时间作为时间戳存储在数据库中。 我发现这个代码:Php 计票年龄,php,time,Php,Time,我正在创建一个票证系统,现在我正在尝试回显票证的年代。 票证日期/时间作为时间戳存储在数据库中。 我发现这个代码: function time_elapsed_string($datetime, $full = false) { $now = new DateTime; $ago = new DateTime($datetime); $diff = $now->diff($ago); $diff->w = floor($diff->d / 7)
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
在本页上:
我说的是格拉维奇的答案。
他说:
但我得到的唯一输出是3天,或1小时。
我需要的输出是:
Only minutes old: 45 minutes
Hours and minutes old: 2 hours 22 minutes
Days, hours and minutes old: 3 days 3 hours (don't show the minutes)
当它超过一个月或一年时,它应该在45天22小时或586天4小时这样的日子里继续回响它
这可能吗?我真的希望我的问题足够清楚,谢谢你的帮助 如果创建
Datetime
对象,可以使用diff()
函数来实现:
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
// shows the total amount of days (not divided into years, months and days like above)
echo "difference " . $interval->days . " days ";
如果您var\u dump($difference)
您应该得到如下结果:
object(DateInterval)
public 'y' => int 0
public 'm' => int 0
public 'd' => int 20
public 'h' => int 6
public 'i' => int 56
public 's' => int 30
public 'invert' => int 0
public 'days' => int 20
如果创建
Datetime
对象,可以使用diff()
函数来实现:
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
// shows the total amount of days (not divided into years, months and days like above)
echo "difference " . $interval->days . " days ";
如果您var\u dump($difference)
您应该得到如下结果:
object(DateInterval)
public 'y' => int 0
public 'm' => int 0
public 'd' => int 20
public 'h' => int 6
public 'i' => int 56
public 's' => int 30
public 'invert' => int 0
public 'days' => int 20
我现在使用的代码:
function time_elapsed_string($ptime){
$date1 = new DateTime(date("Y-m-d H:i", $ptime));
$date2 = new DateTime(date("Y-m-d H:i", time()));
$interval = $date1->diff($date2);
if($interval->days > 0){ $result = $interval->days . ' d ';$result .= $interval->h . ' u '; } else { $result = $interval->h . ' u '; }
if(($interval->days < 1) && ($interval->h < 1)){ $result = $interval->i . ' m'; } else { $result .= $interval->i . ' m'; }
return $result;
}
函数时间\u经过\u字符串($ptime){
$date1=新的日期时间(日期(“Y-m-d H:i”,$ptime));
$date2=新的日期时间(日期(“Y-m-DH:i”,time());
$interval=$date1->diff($date2);
如果($interval->days>0){$result=$interval->days.d';$result.=$interval->h.u';}否则{$result=$interval->h.u';}
如果(($interval->days<1)和($interval->h<1)){$result=$interval->i.m';}其他{$result.=$interval->i.m';}
返回$result;
}
我现在使用的代码:
function time_elapsed_string($ptime){
$date1 = new DateTime(date("Y-m-d H:i", $ptime));
$date2 = new DateTime(date("Y-m-d H:i", time()));
$interval = $date1->diff($date2);
if($interval->days > 0){ $result = $interval->days . ' d ';$result .= $interval->h . ' u '; } else { $result = $interval->h . ' u '; }
if(($interval->days < 1) && ($interval->h < 1)){ $result = $interval->i . ' m'; } else { $result .= $interval->i . ' m'; }
return $result;
}
函数时间\u经过\u字符串($ptime){
$date1=新的日期时间(日期(“Y-m-d H:i”,$ptime));
$date2=新的日期时间(日期(“Y-m-DH:i”,time());
$interval=$date1->diff($date2);
如果($interval->days>0){$result=$interval->days.d';$result.=$interval->h.u';}否则{$result=$interval->h.u';}
如果(($interval->days<1)和($interval->h<1)){$result=$interval->i.m';}其他{$result.=$interval->i.m';}
返回$result;
}
将您的代码稍作调整后用于新答案。将您的代码稍作调整后用于新答案。