PHP date():分钟没有前导零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4639047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP date(): minutes without leading zeros
提问by Bojangles
I'd like to know if there is a formatting letter for PHP's date()
that allows me to print minutes withoutleading zeros, or whether I have to manually test for and remove leading zeros?
我想知道是否有 PHP 的格式字母date()
可以让我打印没有前导零的分钟,或者我是否必须手动测试并删除前导零?
回答by AbdullahC
Use:
用:
$minutes = intval(date('i'));
回答by ScottA
For times with more information than just minutes:
对于比几分钟更多信息的时间:
ltrim()
- Strip whitespace (or other characters) from the beginning of a string
ltrim()
- 从字符串的开头去除空格(或其他字符)
ltrim(date('i:s'), 0);
returns:
返回:
8:24
回答by Madmenyo
I tried to find this for seconds as well, gave up the search and just casting the result as a int like this:
我也试图找到它几秒钟,放弃了搜索,只是将结果转换为这样的 int:
echo (int)date("s");
That will get rid of the leading zero's in a fast efficient way.
这将以快速有效的方式摆脱前导零。
回答by Luke Stevenson
According to the PHP Documentation, the date()
function does not have a placeholder for minutes without leading zeros.
根据PHP 文档,该date()
函数没有前导零的分钟占位符。
You could, however, get that information by simply multiplying the dates, with a leading zero, by 1, turning it into an integer.
但是,您可以通过简单地将带有前导零的日期乘以 1 将其转换为整数来获取该信息。
$minutesWithoutZero = 1* date( 'i' );
回答by alex
Doesn't look like it, but you could do something like...
看起来不像,但你可以做一些像......
echo date('g:') . ltrim(date('i'), '0');
Alternately, you could cast the second call to date()
with (int)
.
或者,您可以将第二个调用转换为date()
with (int)
。
回答by Starx
This also works
这也有效
$timestamp = time(); // Or Your timestamp. Skip passing $timestamp if you want current time
echo (int)date('i',$timestamp);
回答by Etienne Dupuis
I use this format if I need a XXmXXs format:
如果我需要 XXmXXs 格式,我会使用这种格式:
//Trim leading 0's and the 'm' if no minutes
ltrim(ltrim(gmdate("i\ms\s", $seconds), '0'), 'm');
This will output the following:
这将输出以下内容:
12m34s
1m23s
12s
回答by Smit kalwal
i just did this one line solution
我只是做了这一行解决方案
$min = intval(date('i',strtotime($date)));
$min = intval(date('i',strtotime($date)));
Using ltrim method may remove all the leading zeroes.For ex if '00' min.In this case this will remove all the zeroes and gives you empty result.
使用 ltrim 方法可能会删除所有前导零。例如,如果 '00' min。在这种情况下,这将删除所有零并为您提供空结果。
回答by raarts
My solution:
我的解决方案:
function seconds2string($seconds) {
if ($seconds == 0) {
return '-';
}
if ($seconds < 60) {
return date('0:s', $seconds);
}
if ($seconds < 3600) {
return ltrim(date('i:s', $seconds), 0);
}
return date('G:i:s', $seconds);
}
This will output:
这将输出:
0 seconds: -
10 seconds: 0:10
90 seconds: 1:30
301 seconds: 5:01
1804 seconds: 30:04
3601 seconds: 1:00:01
回答by Edgars WEBHAUS
A quickie from me. Tell me what you think:
来自我的快速。告诉我你的想法:
<?php function _wo_leading_zero($n) {
if(!isset($n[1])) return $n;
if(strpos($n, '.') !== false) {
$np = explode('.', $n); $nd = '.';
}
if(strpos($n, ',') !== false) {
if(isset($np)) return false;
$np = explode(',', $n); $nd = ',';
}
if(isset($np) && count($np) > 2) return false;
$n = isset($np) ? $np[0] : $n;
$nn = ltrim($n, '0');
if($nn == '') $nn = '0';
return $nn.(isset($nd) ? $nd : '').(isset($np[1]) ? $np[1] : '');
}
echo '0 => '._wo_leading_zero('0').'<br/>'; // returns 0
echo '00 => '._wo_leading_zero('00').'<br/>'; // returns 0
echo '05 => '._wo_leading_zero('05').'<br/>'; // returns 5
echo '0009 => '._wo_leading_zero('0009').'<br/>'; //returns 9
echo '01 => '._wo_leading_zero('01').'<br/>'; //returns 1
echo '0000005567 => '._wo_leading_zero('0000005567').'<br/>'; //returns 5567
echo '000.5345453 => '._wo_leading_zero('000.5345453').'<br/>'; //returns 0.5345453
echo '000.5345453.2434 => '._wo_leading_zero('000.5345453.2434').'<br/>'; //returns false
echo '000.534,2434 => '._wo_leading_zero('000.534,2434').'<br/>'; //returns false
echo date('m').' => '._wo_leading_zero(date('m')).'<br/>';
echo date('s').' => '._wo_leading_zero(date('s')).'<br/>'; ?>