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?

Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • 5
    May 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
  • 1
    see my answer below... `abs( date( 'i' ) )` does not have the same issue as `intval()` – aequalsb May 01 '19 at 20:01

15 Answers15

109

Use:

$minutes = intval(date('i'));
AbdullahC
  • 6,649
  • 3
  • 27
  • 43
  • 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 07 '15 at 09:52
  • 10
    It'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
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
LF00
  • 27,015
  • 29
  • 156
  • 295
ScottA
  • 1,751
  • 1
  • 11
  • 13
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' );
Luke Stevenson
  • 10,357
  • 2
  • 26
  • 41
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.

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

alex
  • 479,566
  • 201
  • 878
  • 984
3

This also works

$timestamp = time(); // Or Your timestamp. Skip passing $timestamp if you want current time
echo (int)date('i',$timestamp);
Starx
  • 77,474
  • 47
  • 185
  • 261
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
Etienne Dupuis
  • 13,548
  • 6
  • 47
  • 58
  • 1
    Awesome! 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
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.

Smit kalwal
  • 422
  • 2
  • 13
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
raarts
  • 2,711
  • 4
  • 25
  • 45
1

Just use this:

(int) date('i');
user3890355
  • 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
0
$current_date = Date("n-j-Y");
echo $current_date;

// Result m-d-yy

9-10-2012
Bojangles
  • 99,427
  • 50
  • 170
  • 208
bariyaw
  • 41
  • 3
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
    ~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
0

use PHP's absolute value function:

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

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

aequalsb
  • 3,765
  • 1
  • 20
  • 21
0

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

select f1, ..., date_format( fldTime , '%i' ) * 1  as myTime, ..., ...
Manny
  • 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 02 '12 at 00:35
-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

TarangP
  • 2,711
  • 5
  • 20
  • 41