场景:记录已输入数据库。如何用PHP获得午夜时间的小时数
我试图找出下列公式:
- 如何获得的小时数,因为加入的记录。
- 如何获得自添加记录 以来一直保留到午夜的时间。
鉴于这些时间:
- 日期/时间:2012-08-22 20时11分二十秒
- 时间戳:1345684280
- 今晚午夜:2012年8月23日00:00:00
- 午夜时间戳:1345698000
我觉得就像我在正确的轨道上。只需要一些适当的数学来做计算?我在数学上很可怕。任何帮助或指导,将不胜感激。我不想找人为我完成这个任务。只是寻找我做错的建议,或者我可以做得更好。也许可以解释实现我的目标所需的数学公式。
这是我到目前为止有:
class tools{
public function __construct(){
}
public function check_time($time, $request){
$time = strtotime($time);
if($request == 'since'){
$theTime = time() - $time;
$prefix = 'Since:';
} elseif($request == 'until'){
$midnight = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
$theTime = $midnight - $time;
$prefix = 'Until:';
}
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach($tokens as $unit => $text){
if($theTime < $unit) continue;
$duration = floor($theTime/$unit);
return $prefix.' '.$duration.' '.$text.(($duration>1)?'s':'');
}
}
}// EoF tools class
$tools = new tools();
print_r($tools->check_time('2012-08-22 20:11:20', 'since'));
print_r($tools->check_time('2012-08-22 20:11:20', 'until'));
或简单strtotime('明天')每http://www.php.net/manual/en/datetime.formats.relative.php –
感谢您的解释。这对我非常有用 – xzdead