我有一个简单的脚本,动态地将分钟动态转换为分钟,小时和天(意思是如果我有1分钟,脚本将放置“1分钟”,如果我有61分钟,脚本将说“ 1小时和1分钟“等,具有正确的语法)。基本上,虽然我尝试将变量的值设置为int,但我当前的时间(112分钟)为:1.8666666666667小时和52分钟。
settype($otime, "integer");
settype($hours, "integer");
settype($mins, "integer");
settype($hour, "integer");
if($otime == 1) {
$otime = "1 Minute";
}else if($otime < 60 && $otime !=1) {
$otime = $otime." Minutes";
}
else if($otime >= 60 && $otime < 1440) {
$hours = $otime / 60;
$mins = $otime % 60;
if($mins == 1 && $hours != 1) {
$otime = $hours." Hours and ".$mins." Minute";
}
else if($mins != 1 && $hours == 1) {
$otime = $hours." Hour and ".$mins." Minutes";
}
else if($mins == 1 && $hours == 1) {
$otime = $hours." Hour and ".$mins." Minute";
}
else {
$otime = $hours." Hours and ".$mins." Minutes";
}
}
else if($otime >= 1440) {
$days = $otime / 1440;
$hour = $otime % 1440;
$hours = $hour / 60;
$mins = $hour % 60;
if($days == 1 && $hours != 1 && $mins != 1) {
$otime = $days." Day ".$hours." Hours and ".$mins." Minutes";
}
if($days != 1 && $hours == 1 && $mins != 1) {
$otime = $days." Days ".$hours." Hour and ".$mins." Minutes";
}
if($days != 1 && $hours != 1 && $mins == 1) {
$otime = $days." Days ".$hours." Hours and ".$mins." Minute";
}
if($days == 1 && $hours == 1 && $mins != 1) {
$otime = $days." Day ".$hours." Hour and ".$mins." Minutes";
}
if($days == 1 && $hours == 1 && $mins == 1) {
$otime = $days." Day ".$hours." Hour and ".$mins." Minute";
}
if($days != 1 && $hours == 1 && $mins == 1) {
$otime = $days." Days ".$hours." Hour and ".$mins." Minute";
}
if($days == 1 && $hours != 1 && $mins == 1) {
$otime = $days." Day ".$hours." Hours and ".$mins." Minute";
}
if($days != 1 && $hours != 1 && $mins != 1) {
$otime = $days." Days ".$hours." Hours and ".$mins." Minutes";
}
}
另外 - 有更简单的方法吗?我是否错过任何if(用于语法目的)
答案 0 :(得分:1)
尝试这样的事情:
echo gmdate("H:i:s", ($minutes * 60));
或使用函数DateTime() :(感谢@Ryan Kempt)
function minutesToTime($minutes) {
$minutes = $minutes * 60;
$dtF = new DateTime("@0");
$dtT = new DateTime("@$minutes");
if($dtF->diff($dtT)->format('%i') !=0 ){
$min = ($dtF->diff($dtT)->format('%i') == 1 ? ' %i Minute' : ' %i Minutes');
}
else{
$min = '';
}
if($dtF->diff($dtT)->format('%h') !=0 ){
$hours = ($dtF->diff($dtT)->format('%h') == 1 ? ' %h Hour' : ' %h Hours');
}
else{
$hours = '';
}
if($dtF->diff($dtT)->format('%a') !=0 ){
$days = ($dtF->diff($dtT)->format('%a') == 1 ? ' %a Day' : ' %a Days');
}
else{
$days = '';
}
if($dtF->diff($dtT)->format('%s') !=0 ){
$seconds = ($dtF->diff($dtT)->format('%s') == 1 ? ' %s Second' : ' %s Seconds');
}
else{
$seconds = '';
}
return $dtF->diff($dtT)->format($days.' '.$hours.' '.$min.' '.$seconds.'');
}
echo minutesToTime(456543.5);
<强>输出强>
1317 Days, 1 Hour, 3 Minutes and 30 Seconds
答案 1 :(得分:0)
您的问题是您在设置变量类型之前,而不是变量具有值。
对于$ hours,请在分割后设置类型。替换
行$hours = $otime / 60;
有:
$hours = $otime / 60;
settype($hours, "integer");
您可能需要使用其他变量$ mins和$ hour来执行此操作
答案 2 :(得分:0)
有一种更简单的方法可以在没有太多if语句的情况下执行此操作,但也提供正确的复数语言。
echo minutes2human(1211);
function minutes2human($mins) {
$m = floor($mins % 60);
$h = floor(($mins % 1440) / 60);
$d = floor($mins / 1440);
$m_word = ($m == 1 ? 'Minute' : 'Minutes');
$h_word = ($h == 1 ? 'Hour' : 'Hours');
$d_word = ($d == 1 ? 'Day' : 'Days');
return "$d $d_word $h $h_word and $m $m_word";
}
我们通过检查分钟是1,小时是1,以及天是1,然后将它们全部合并为一个字符串来减少很多if语句。而不是检查每个可能的情况(如果分钟是1而小时不是1而天不是1,如果分钟是1,小时是1,天不是1)等等......