如何使用PHP获取午夜前的小时数
场景:数据库中输入了一条记录 我正试图找出以下方程式:如何使用PHP获取午夜前的小时数,php,math,datetime,timestamp,equation,Php,Math,Datetime,Timestamp,Equation,场景:数据库中输入了一条记录 我正试图找出以下方程式: class tools{ public function __construct(){ } public function check_time($time, $request){ $time = strtotime($time); if($request == 'since'){ $theTime = time() - $time;
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'));
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'));
- 日期/时间:2012-08-22 20:11:20
- 时间戳:1345684280
- 今晚午夜:2012-08-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'));
类工具{
公共函数构造(){
}
公共功能检查时间($time,$request){
$time=strottime($time);
如果($request=='since'){
$theTime=time()-$time;
$prefix='自:';
}elseif($request=='until'){
$midnight=mktime(0,0,0,日期('n')、日期('j')、日期('Y'));
$theTime=$midnight-$time;
$prefix='直到:';
}
$tokens=数组(
31536000=>“年”,
2592000=>“月”,
604800=>“一周”,
86400=>“天”,
3600=>“小时”,
60=>“分钟”,
1=>“秒”
);
foreach($unit=>$text形式的令牌){
如果($时间<$单位)继续;
$duration=楼层($theTime/$unit);
返回$prefix.'.$duration.'.$text.(($duration>1)?'s':'';
}
}
}//EoF工具类
$tools=新工具();
打印($tools->check_time('2012-08-22 20:11:20','since');
打印($tools->check_time('2012-08-22 20:11:20','until');
您可以尝试以下方法:
$now = strtotime('2012-08-22 20:11:20');
$midnight = strtotime('2012-08-23 00:00:00');
$difference = $midnight - $now;
echo date("H:i:s", $difference);
也许是这样的?我必须承认,我没有完全理解您想要的输出是什么:
$d1 = new DateTime('2012-08-22 20:11:20');
$d2 = new DateTime('2012-08-23 00:00:00');
$interval = $d1->diff($d2);
echo $interval->format('%h hours %i minutes and %s seconds');
如果不指定时间,午夜不得超过第二天。最好的方法是:
<?php
$datetime1 = new DateTime(date('Y-m-d H:i:s'));//current datetime object
$datetime2 = new DateTime(date('Y-m-').date(d));//next day at midnight
$interval = $datetime1->diff($datetime2);//diference
echo $interval->format('H');printing only hours (same as date format)
?>
如果您想了解更多信息:这里的解决方案非常简单。有一个小错误导致了您的所有问题 在你的代码中,你可以计算午夜
$midnight = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
这是不正确的,因为它使用的是今天的午夜(无论从现在起多少个小时之前,今天应该是00:00。您想要明天的午夜,因为它被认为是24小时时间的00:00,并且是明天。正确的方法如下所示:
$midnight = strtotime("tomorrow 00:00:00");
请记住strotime()以GMT为基础,因此请确保在文件/应用程序中设置默认时区
我希望我的答案是清楚的,并解释了为什么你发布的代码是错误的,以及如何修复它。简单明了:
$timeLeft = 86400 - (time() - strtotime("today"));
echo date("H:i:s", $timeLeft);
86400
是一天的初始时间。time()
是当前时间。strotime(“今天”)
是这一天的开始时间
date(“H:i:s,$timeLeft)
用于以小时、分钟和秒为单位的格式设置
更简短的方式:
date("H:i:s", strtotime("tomorrow") - time())
我已经更新了我的问题,以获得更清晰的解释和代码修订。这并没有给我第二天的午夜,但是今天的午夜(datetime2),函数可以吗?或者简单地说是strotime(“明天”),谢谢你的解释。它对我非常有用