I'd like to know if there is a formatting letter for PHP's date()
that allows me to print minutes without leading zeros, or whether I have to manually test for and remove leading zeros?

- 99,427
- 50
- 170
- 208
-
5May I ask WHY the hell there is no minutes without zeros ??? – Jackt Feb 02 '18 at 21:41
-
Yes please ask .. i want to hear that answer too ... – Benjamin Eckstein Jun 27 '18 at 10:17
-
One reason: If you output the result of eg date("Y,n,j,G,i,s", xxx) to javascript, it'll throw a warning on octal numbers on 08 & 09 minutes & seconds. Although this can be worked around by passing quoted: date("'Y','n','j','G','i','09'", xxx) – Steve Horvath Apr 18 '19 at 00:27
-
1see my answer below... `abs( date( 'i' ) )` does not have the same issue as `intval()` – aequalsb May 01 '19 at 20:01
15 Answers
Use:
$minutes = intval(date('i'));

- 6,649
- 3
- 27
- 43
-
6Crazy PHP behaviour! It treats the int `09` as an octal number because of the leading zero - which will evaluate to `0` since `9` isn't a valid octal number. But it treats the string `09` as the decimal integer `9`. Good bye expected behaviour! :) – hek2mgl Apr 07 '15 at 09:52
-
10It's better to use `(int)date('i');` instead of `intval()`. It's supposed to be much faster. – KoviNET Oct 25 '17 at 13:23
-
Well, according to documentation, this is an expected behaviour due to second parameter of intval which is `base` and by default it is equal to 10. PHP will treat input an octal number if base explicitly defined as `0` or `8` – Rulisp Nov 26 '18 at 03:34
-
@hek2mgl see my answer below... `abs( date( 'i' ) )` does not have the same issue as `intval()` – aequalsb May 01 '19 at 20:00
-
`echo +'05';` results in printing the value `5`. So `+$date->format("your-format")` will return an integer :) – Eugen Mihailescu Oct 24 '19 at 21:15
For times with more information than just minutes:
ltrim()
- Strip whitespace (or other characters) from the beginning of a string
ltrim(date('i:s'), 0);
returns:
8:24
-
3this doesn't work on times less then 1 minute.. for example: 00:24 would result in :24 – Ludo - Off the record Jul 28 '14 at 15:53
-
Perfectly fits into my function (..`elseif ($duration <= 3599 && $duration > 59) {`..) – Jasom Dotnet Nov 11 '15 at 15:19
-
It's a good solution but just a bad example, it should be just `ltrim(date('i'), 0)` and `ltrim(date('s'), 0)` separately :) – jave.web Nov 05 '18 at 19:49
According to the PHP Documentation, the date()
function does not have a placeholder for minutes without leading zeros.
You could, however, get that information by simply multiplying the dates, with a leading zero, by 1, turning it into an integer.
$minutesWithoutZero = 1* date( 'i' );

- 10,357
- 2
- 26
- 41
-
True. Just using bad old habits picked up in my javascript days. – Luke Stevenson Jan 09 '11 at 12:24
-
+1 for the novel solution Lucanos. Thanks. I've gone with `Hippo`'s answer due to it's simplicity and non-hackiness ;-P – Bojangles Jan 09 '11 at 12:24
I tried to find this for seconds as well, gave up the search and just casting the result as a int like this:
echo (int)date("s");
That will get rid of the leading zero's in a fast efficient way.

- 8,389
- 7
- 52
- 99
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)
.

- 479,566
- 201
- 878
- 984
This also works
$timestamp = time(); // Or Your timestamp. Skip passing $timestamp if you want current time
echo (int)date('i',$timestamp);

- 77,474
- 47
- 185
- 261
-
4
-
2@alex, I was actually trying to show where he could add his own timestamp – Starx Jan 09 '11 at 12:29
-
alex is right... `date( 'format_string' )` assumes `time()` as the second argument – aequalsb Nov 17 '19 at 16:07
I use this format if I need a XXmXXs format:
//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

- 13,548
- 6
- 47
- 58
-
1Awesome! Even better then @ScottA solution. This is my code (also with hours) `print ltrim(gmdate("H\hi\ms\s", $duration), '0mh');` – Jasom Dotnet Nov 11 '15 at 15:29
i just did this one line solution
$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.

- 422
- 2
- 13
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

- 2,711
- 4
- 25
- 45
Just use this:
(int) date('i');

- 1,010
- 13
- 13
-
@KrzysztofJaniszewski Thanks for response. Sorry, but my native language is not english, so for me hard to explain something in english. But I will try. `(int)` function works native in PHP. So it should works fast. But I didn't test it. – user3890355 Jul 04 '18 at 11:52
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/>'; ?>

- 84
- 3
-
1~12 function calls to remove leading zeros. Your function could be broken down into this `(int) "001"` – naT erraT Apr 10 '19 at 16:56
use PHP's absolute value function:
abs( '09' ); // result = 9
abs( date( 'i' ) ); // result = minutes without leading zero

- 3,765
- 1
- 20
- 21
Or in mySQL just multiply it by 1, like such:
select f1, ..., date_format( fldTime , '%i' ) * 1 as myTime, ..., ...

- 1
-
1I didn't ask for a MySQL solution, but thank you for the alternative - I'll keep this in mind for MySQL-related formatting. – Bojangles Mar 02 '12 at 00:35
My Suggestion is Read this beautiful documentation it have all details of php date functions
And as per your question you can use i - Minutes with leading zeros (00 to 59)
Which return you minutes with leading zero(0).
And Also introducing [Intval()][2]
function returns the integer value of a variable. You can not use the intval() function on an object

- 2,711
- 5
- 20
- 41