当前位置: 动力学知识库 > 问答 > 编程问答 >

localization - PHP Intl library and human readable time span

问题描述:

I'm using PHP Intl library to format dates, numbers and so on.

Now I have to show a time span in human readable string, for example:

1day, 1hour, 10 minutes, 14s

or in compact way:

1d 1s 10m 14s

or in supercompact way:

1d 1.10:14

Is there any way to do this using the build in Intl library without gettext?

If no, I can format the timespan with a custom function, but can I get at least the names ('day', or 'd') in the correct locale from IntlDateFormatter?

网友答案:

Yes, answer for your question is DateInterval. According to documentation you can use it this way:

$pastDate = new DateTime('2015-09-01 15:00:00');
$today = new DateTime();

//calculate interval and returns DateInterval object
$timeSpan = $today->diff($pastDate); 

//now we'll format it
echo $timeSpan->format('%a day, %h hour, %i minutes, %ss').PHP_EOL;
echo $timeSpan->format('%ad %hh %im %ss').PHP_EOL;
echo $timeSpan->format('%ad %h.%i:%s').PHP_EOL;

The result will be:

100 day, 9 hour, 55 minutes, 12s

100d 9h 55m 12s

100d 9.55:12

分享给朋友:
您可能感兴趣的文章:
随机阅读: