繁体   English   中英

在PHP中将时间格式转换为文本

[英]Converting time format to text in php

我有一个时间说"01:30:00"我想将其转换为one hour,thirty mins,zero seconds

我试图通过添加."hours".",". ."mins"来将数字转换为文本."hours".",". ."mins" ."hours".",". ."mins"但是还有其他方法来获取数据,例如时间到单词吗?

快速的Google搜索找到了答案

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

    /** 
     * 
     * @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); 

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM