給瀏覽者一個不準確的時間 – 馬永占 譯

來源:互聯網
上載者:User

著作權聲明:原創作品,允許轉載,轉載時請務必以超連結形式標明文章原始出版、作者資訊和本聲明。否則將追究法律責任。http://blog.csdn.net/mayongzhan - 馬永占,myz,mayongzhan

給瀏覽者一個不準確的時間

SitePoint上一篇文章
http://www.sitepoint.com/blogs/2008/03/06/give-you-visitors-a-rough-time/
Toby Somerville 發表了一篇名為:RoughTime的文章,文章中指出他建議在頁面上顯示更人性化的粗略的時間.
當問及時間問題,我們不需要一個非常準確的時間.[...]一般來來說,我們在日常生活工作中只需要知道一個大概的時間.例如:快十點了.剛過三點 半...現在在網上一般都顯示12:24:13AM,或者類似的準確時間,這非常不友好.(譯者注:其實寫時間都沒什麼作用,誰的作業系統下面沒有時 間???)
作者用了兩個switch語句將小時和分鐘分別用不同的字串代替.例如:在15到20中間就是一刻鐘,小時方面是把數字轉換成英文(one,two,three,中文的話,就是十二點,十點. )
 
附程式
< ?php
$hour = (int) date('g');
$minute = (int) date('i');

switch($minute){
    case ($minute >=0 && $minute <5):
        $roughTime = numberToWord($hour).'-ish';
    break;
    case ($minute >=5 && $minute <14):
        $roughTime = 'just gone '.numberToWord($hour);
    break;
    case ($minute >=15 && $minute <20):
        $roughTime = 'quarter past '.numberToWord($hour);
    break;
    case ($minute >=20 && $minute <25):
        $roughTime = 'nearly half '.numberToWord($hour);
    break;
    case ($minute >=25 && $minute <35):
        $roughTime = 'half '.numberToWord($hour);
    break;
    case ($minute >=35 && $minute <40):
        $roughTime = 'just gone half '.numberToWord($hour);
    break;
    case ($minute >=40 && $minute <50):
        $roughTime = 'quarter to '.numberToWord($hour + 1);
    break;
    case ($minute >=50):
        $roughTime =  'nearly '.numberToWord($hour +1);
    break;
}

function numberToWord($number){
    switch($number){
        case 1:
            $word = 'one';break;
        case 2:
            $word = 'two';break;
        case 3:
            $word = 'three';break;
        case 4:
            $word = 'four';break;
        case 5:
            $word = 'five';break;
        case 6:
            $word = 'six';break;
        case 7:
            $word = 'seven';break;
        case 8:
            $word = 'eight';break;
        case 9:
            $word = 'nine';break;
        case 10:
            $word = 'ten';break;
        case 11:
            $word = 'eleven';break;
        case 13:
            $word = 'one';break;
        default:
            $word = 'twelve';break;
    }
    return $word;
}

echo 'Its '.$roughTime;
?>

 

 

SitePoint WebTech Blog: Give Your Visitors a Rough Time

In a new post to the Web Tech blog over on SitePoint, Toby Somerville has posted something he calls "RoughTime" - his method for displaying a more "human friendly" output of a timestamp.
When asked the time, we generally don't need to be military accurate with our response [...] we generally communicate the approximate time. i.e. 'its nearly ten' or 'its just gone half past three'. Yet on the web, time is generally shown as '12:24:13 AM', or similar â" not very visitor friendly.
His RoughTime uses two switch statements to map the hour and minute values to different strings. For example, minutes between 15 and 20 become "quarter past" and the hours are changed over from numeric versions to their word counterparts.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.