60

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?

4
  • 6
    May I ask WHY the hell there is no minutes without zeros ???
    – Jackt
    Feb 2, 2018 at 21:41
  • 1
    Yes please ask .. i want to hear that answer too ... Jun 27, 2018 at 10:17
  • 1
    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) Apr 18, 2019 at 0:27
  • 1
    see my answer below... abs( date( 'i' ) ) does not have the same issue as intval()
    – aequalsb
    May 1, 2019 at 20:01

15 Answers 15

109

Use:

$minutes = intval(date('i'));
5
  • 6
    Crazy 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 7, 2015 at 9:52
  • 10
    It's better to use (int)date('i'); instead of intval(). It's supposed to be much faster.
    – KoviNET
    Oct 25, 2017 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, 2018 at 3:34
  • @hek2mgl see my answer below... abs( date( 'i' ) ) does not have the same issue as intval()
    – aequalsb
    May 1, 2019 at 20:00
  • echo +'05'; results in printing the value 5. So +$date->format("your-format") will return an integer :) Oct 24, 2019 at 21:15
20

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
3
  • 3
    this doesn't work on times less then 1 minute.. for example: 00:24 would result in :24 Jul 28, 2014 at 15:53
  • Perfectly fits into my function (..elseif ($duration <= 3599 && $duration > 59) {..) Nov 11, 2015 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 5, 2018 at 19:49
8

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' );
2
  • True. Just using bad old habits picked up in my javascript days. Jan 9, 2011 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 9, 2011 at 12:24
7

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.

4

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).

0
3

This also works

$timestamp = time(); // Or Your timestamp. Skip passing $timestamp if you want current time
echo (int)date('i',$timestamp);
3
  • 4
    No need to add time() there.
    – alex
    Jan 9, 2011 at 12:27
  • 2
    @alex, I was actually trying to show where he could add his own timestamp
    – Starx
    Jan 9, 2011 at 12:29
  • alex is right... date( 'format_string' ) assumes time() as the second argument
    – aequalsb
    Nov 17, 2019 at 16:07
2

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
1
  • 1
    Awesome! Even better then @ScottA solution. This is my code (also with hours) print ltrim(gmdate("H\hi\ms\s", $duration), '0mh'); Nov 11, 2015 at 15:29
2

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.

1

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
1

Just use this:

(int) date('i');
1
  • @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. Jul 4, 2018 at 11:52
0

Or in mySQL just multiply it by 1, like such:

select f1, ..., date_format( fldTime , '%i' ) * 1  as myTime, ..., ...
1
  • 1
    I 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 2, 2012 at 0:35
0
$current_date = Date("n-j-Y");
echo $current_date;

// Result m-d-yy

9-10-2012
0
0

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/>'; ?>
1
  • 1
    ~12 function calls to remove leading zeros. Your function could be broken down into this (int) "001"
    – naT erraT
    Apr 10, 2019 at 16:56
0

use PHP's absolute value function:

abs( '09' ); // result = 9

abs( date( 'i' ) ); // result = minutes without leading zero

-2

My Suggestion is Read this beautiful documentation it have all details of php date functions

Link of Documentation

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

1
  • 3
    OP wants minutes WITHOUT leading zeros
    – aequalsb
    May 1, 2019 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.