0

我有这个HH:mm:ss总时间格式,我想把它转换成单词,例如

01:00:00 = 1小时
00:30:00 = 30 分钟
00:00:30 = 30 秒
01:30:30 = 1 小时 30 分 30 秒

有什么帮助吗?

4

4 回答 4

1

我在这里找到了一个很好的例子。

<?php
/**
 *
 * @convert seconds to words
 *
 * @param INT $seconds
 *
 * @return string
 *
 */
function secondsToWords($seconds) {
    /*** number of days ***/
    $days = (int)($seconds / 86400);
    /*** if more than one day ***/
    $plural = $days > 1 ? 'days' : 'day';
    /*** number of hours ***/
    $hours = (int)(($seconds - ($days * 86400)) / 3600);
    /*** number of mins ***/
    $mins = (int)(($seconds - $days * 86400 - $hours * 3600) / 60);
    /*** number of seconds ***/
    $secs = (int)($seconds - ($days * 86400) - ($hours * 3600) - ($mins * 60));
    /*** return the string ***/
    return sprintf("%d $plural, %d hours, %d min, %d sec", $days, $hours, $mins, $secs);
}

/*** example usage ***/

/*** start time ***/
$start = strtotime('10:30 January 7 2004');
/*** time now in seconds ***/
$now = time();
/*** do the math ***/
$seconds = $now - $start;

/*** show the words ***/
echo secondsToWords($seconds);
?>

输出:

3510 days, 10 hours, 17 min, 39 sec

于 2013-08-18T01:49:08.867 回答
1

使用explode()拆分字符串,您将获得小时、分钟、秒的值。

list ($hour, $minute, $second) = explode (':', $timestamp);
于 2013-08-18T01:49:09.277 回答
0

有很多解决方案。一个简单的解决方案是获取您想要的数据的两个字符并检查它是否不同于“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;

这给了我们 1hour 2minutes 3seconds

(不是说你需要在之前设置条件,因为在原始值为 00:00:00 的情况下,此脚本将不显示任何内容)

于 2013-08-18T01:51:16.833 回答
0

好的,现在是旧消息,但我只需要编写一个函数来将编号时间转换为单词:

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;
}

14:33 将返回:27 分钟到下午 3 点

于 2020-02-29T20:18:50.763 回答