如何在PHP中将时间转换为单词?
我有这个如何在PHP中将时间转换为单词?,php,time,timestamp,Php,Time,Timestamp,我有这个HH:mm:sstotal time格式,我想把它转换成单词 e、 g 01:00:00=1小时 00:30:00=30分钟 00:00:30=30秒 01:30:30=1小时30分钟30秒 有什么帮助吗?我找到了一个很好的例子 输出: 3510天、10小时、17分钟、39秒用于拆分字符串,您将获得小时、分钟、秒的值 list ($hour, $minute, $second) = explode (':', $timestamp); 有很多解决方案。 一个简单的解决方案是获取所需
HH:mm:ss
total time格式,我想把它转换成单词
e、 g
01:00:00=1小时00:30:00=30分钟
00:00:30=30秒
01:30:30=1小时30分钟30秒 有什么帮助吗?我找到了一个很好的例子
输出:
3510天、10小时、17分钟、39秒
用于拆分字符串,您将获得小时、分钟、秒的值
list ($hour, $minute, $second) = explode (':', $timestamp);
有很多解决方案。 一个简单的解决方案是获取所需数据的两个字符,并检查它是否与“00”不同,如果不同,则添加到输出字符串中
$original = '01:02:03';
$out = '';
if(substr($original,0,2) == '01') //Here we check if this is equal to 1 cause this is the only cas where we will write "hour" and not "hours"
$out .= ((int)substr($original,0,2)) . 'hour ';//Here we cast our value to int to remove the 0 before the value
else if(substr($original,0,2) != '00') // In all others cases excepted when there is 0 hours
$out .= ((int)substr($original,0,2)) . 'hours ';
if(substr($original,3,2) == '01')
$out .= ((int)substr($original,3,2)) . 'minute ';
else if(substr($original,3,2) != '00')
$out .= ((int)substr($original,3,2)) . 'minutes ';
if(substr($original,6,2) == '01')
$out .= ((int)substr($original,6,2)) . 'second ';
else if(substr($original,6,2) != '00')
$out .= ((int)substr($original,6,2)) . 'seconds ';
echo $out;
这给了我们1小时2分钟3秒
(不是说你需要在之前设置一个条件,因为在原始值为00:00:00的情况下,此脚本将不显示任何内容)好的,现在是老消息,但我只需要编写一个函数来将编号的时间转换为单词:
define("MILITARY_TIME",false);
date_default_timezone_set('America/Los_Angeles');
$arr = array((int)date("H"),(int)date("i"));
tellTimeInWords($arr);
function tellTimeInWords($arr){
$hours = $arr[0];
$minutes = $arr[1];
$num_word_mid = array(
11 =>"Eleven",
12 =>"Twelve",
13 =>"Thirteen",
14 =>"Fourteen",
15 =>"Fifteen",
16 =>"Sixteen",
17 =>"Seventeen",
18 =>"Eighteen",
19 =>"Nineteen");
$num_word_single = array(
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine");
$num_word_double = array(2=>"Twenty ",
3=>"Thirty ",
4=>"Fourty ",
5=>"Fifty ");
$marr = array(15,30,45,00);
$marr_r = array("Quarter Past ","Half Past ","Quarter To "," O' Clock");
$exact = str_replace($marr,$marr_r,$minutes);
$suffix = " AM";
if($hours >= 12){
$suffix = " PM";
if(!MILITARY_TIME && $hours!==12)
$hours = $hours-12;
}
$m = "";
$h = $hours;
if($minutes >30){
if(!in_array($minutes,$marr)){
$m = 60 - $minutes;
$s = ($m>1)? "s":"";
$exact = " Minute$s To ";
}
$h = $h+1;
}
if($arr[1] < 30){
if(!in_array($minutes,$marr)){
$m = $arr[1];
$s = ($m>1)? "s":"";
$exact = " Minute$s Past ";
}
}
$m = conv_word($m,0,$num_word_mid,$num_word_single,$num_word_double);
$h = conv_word($h,1,$num_word_mid,$num_word_single,$num_word_double);
if($minutes == 0)
echo $h.$exact;
else
echo $m. $exact. $h.$suffix;
}
function conv_word($m,$h,$num_word_mid,$num_word_single,$num_word_double){
if($m < 1)
return;
if($m==24 && $h) return "midnight";
if(strlen($m) == 1){
$m = $num_word_single[$m-1];
}
else if($m > 9 && $m < 20){
$m = $num_word_mid[$m];
}else{
$nums = array_map('intval', str_split($m));
$m = $num_word_double[$nums[0]].$num_word_single[$nums[1]-1];
}
return $m;
}
定义(“军事时间”,假);
日期默认时区设置(“美国/洛杉矶”);
$arr=数组((int)日期(“H”),(int)日期(“i”);
tellTimeInWords($arr);
函数tellTimeInWords($arr){
$hours=$arr[0];
$minutes=$arr[1];
$num\u word\u mid=数组(
11=>“11”,
12=>“12”,
13=>“十三”,
14=>“14”,
15=>“十五”,
16=>“16”,
17=>“17”,
18=>“18”,
19=>“19”);
$num\u word\u single=数组(
“一个”,
“两个”,
“三”,
“四”,
“五个”,
“六”,
“七”,
“八”,
“九”);
$num\u word\u double=数组(2=>“二十”,
3=>“三十”,
4=>“四十”,
5=>“五十”);
$marr=数组(15,30,45,00);
$marr_r=数组(“一刻过去”、“半过去”、“一刻到”、“点”);
$exact=str_replace($marr,$marr_r,$minutes);
$suffix=“AM”;
如果($hours>=12){
$suffix=“PM”;
如果(!军事时间&&$hours!==12)
$hours=$hours-12;
}
$m=“”;
$h=$hours;
如果($minutes>30){
if(!in_数组($minutes,$marr)){
$m=60-$min;
$s=($m>1)?“s”:“;
$exact=“分钟$s到”;
}
$h=$h+1;
}
如果($arr[1]<30){
if(!in_数组($minutes,$marr)){
$m=$arr[1];
$s=($m>1)?“s”:“;
$exact=“分钟$s过去”;
}
}
$m=conv_word($m,0,$num_word_mid,$num_word_single,$num_word_double);
$h=conv_word($h,1,$num_word_mid,$num_word_single,$num_word_double);
如果($minutes==0)
echo$h.$exact;
其他的
echo$m.$exact.$h.$后缀;
}
函数conv\u word($m、$h、$num\u word\u mid、$num\u word\u single、$num\u word\u double){
如果($m<1)
返回;
如果($m==24&&$h)返回“午夜”;
如果(strlen($m)==1){
$m=$num_word_single[$m-1];
}
否则,如果($m>9&$m<20){
$m=$num_word_mid[$m];
}否则{
$nums=array_-map('intval',str_-split($m));
$m=$num_word_double[$nums[0].$num_word_single[$nums[1]-1];
}
返还万美元;
}
14:33将返回:
下午三点二十七分
define("MILITARY_TIME",false);
date_default_timezone_set('America/Los_Angeles');
$arr = array((int)date("H"),(int)date("i"));
tellTimeInWords($arr);
function tellTimeInWords($arr){
$hours = $arr[0];
$minutes = $arr[1];
$num_word_mid = array(
11 =>"Eleven",
12 =>"Twelve",
13 =>"Thirteen",
14 =>"Fourteen",
15 =>"Fifteen",
16 =>"Sixteen",
17 =>"Seventeen",
18 =>"Eighteen",
19 =>"Nineteen");
$num_word_single = array(
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine");
$num_word_double = array(2=>"Twenty ",
3=>"Thirty ",
4=>"Fourty ",
5=>"Fifty ");
$marr = array(15,30,45,00);
$marr_r = array("Quarter Past ","Half Past ","Quarter To "," O' Clock");
$exact = str_replace($marr,$marr_r,$minutes);
$suffix = " AM";
if($hours >= 12){
$suffix = " PM";
if(!MILITARY_TIME && $hours!==12)
$hours = $hours-12;
}
$m = "";
$h = $hours;
if($minutes >30){
if(!in_array($minutes,$marr)){
$m = 60 - $minutes;
$s = ($m>1)? "s":"";
$exact = " Minute$s To ";
}
$h = $h+1;
}
if($arr[1] < 30){
if(!in_array($minutes,$marr)){
$m = $arr[1];
$s = ($m>1)? "s":"";
$exact = " Minute$s Past ";
}
}
$m = conv_word($m,0,$num_word_mid,$num_word_single,$num_word_double);
$h = conv_word($h,1,$num_word_mid,$num_word_single,$num_word_double);
if($minutes == 0)
echo $h.$exact;
else
echo $m. $exact. $h.$suffix;
}
function conv_word($m,$h,$num_word_mid,$num_word_single,$num_word_double){
if($m < 1)
return;
if($m==24 && $h) return "midnight";
if(strlen($m) == 1){
$m = $num_word_single[$m-1];
}
else if($m > 9 && $m < 20){
$m = $num_word_mid[$m];
}else{
$nums = array_map('intval', str_split($m));
$m = $num_word_double[$nums[0]].$num_word_single[$nums[1]-1];
}
return $m;
}