【问题标题】:How to convert time to words in PHP?如何在 PHP 中将时间转换为单词?
【发布时间】:2013-08-18 01:41:06
【问题描述】:

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

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

有什么帮助吗?

【问题讨论】:

    标签: php time timestamp


    【解决方案1】:

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

    list ($hour, $minute, $second) = explode (':', $timestamp);
    

    【讨论】:

      【解决方案2】:

      我找到了一个很好的例子here

      <?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

      【讨论】:

        【解决方案3】:

        有很多解决方案。 一个简单的解决方案是获取您想要的数据的两个字符并检查它是否不同于“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的情况下,这个脚本什么都不会显示)

        【讨论】:

          【解决方案4】:

          现在好了,但我只需要编写一个函数将编号时间转换为单词:

          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 将返回: 二十七分到下午三点

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-11-07
            • 2021-04-05
            • 1970-01-01
            • 2021-01-08
            • 1970-01-01
            • 1970-01-01
            • 2017-03-30
            相关资源
            最近更新 更多