PHP date (): minutos sin ceros a la izquierda

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

preguntado el 09 de enero de 11 a las 12:01

May I ask WHY the hell there is no minutes without zeros ??? -

Yes please ask .. i want to hear that answer too ... -

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

see my answer below... abs( date( 'i' ) ) does not have the same issue as intval() -

15 Respuestas


$minutes = intval(date('i'));

Respondido el 09 de enero de 11 a las 15:01

Crazy PHP behaviour! It treats the int 09 as an octal number because of the leading zero - which will evaluate to 0 desde 9 isn't a valid octal number. But it treats the string 09 as the decimal integer 9. Good bye expected behaviour! :) - hek2mgl

Es mejor usar (int)date('i'); en lugar de intval(). It's supposed to be much faster. - KoviNET

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

@hek2mgl see my answer below... abs( date( 'i' ) ) does not have the same issue as intval() - aequalsb

echo +'05'; results in printing the value 5. Así que, +$date->format("your-format") will return an integer :) - Eugen Mihailescu

For times with more information than just minutes:

ltrim() - Quite los espacios en blanco (u otros caracteres) del principio de una cadena

ltrim(date('i:s'), 0);



Respondido el 24 de diciembre de 16 a las 13:12

this doesn't work on times less then 1 minute.. for example: 00:24 would result in :24 - Ludo - Off the record

Perfectly fits into my function (..elseif ($duration <= 3599 && $duration > 59) {..) - Jasom Dotnet

It's a good solution but just a bad example, it should be just ltrim(date('i'), 0) y ltrim(date('s'), 0) separately :) - jave.web

Según el Documentación de PHP,la 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' );

Respondido el 09 de enero de 11 a las 15:01

True. Just using bad old habits picked up in my javascript days. - Luke Stevenson

+1 for the novel solution Lucanos. Thanks. I've gone with Hippo's answer due to it's simplicity and non-hackiness ;-P - Bojangles

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.

Respondido 09 Abr '12, 20:04

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() es (int).

Respondido el 09 de enero de 11 a las 15:01

Awesome! I'm glad it's so simple. Thanks. - Bojangles

Esto también funciona

$timestamp = time(); // Or Your timestamp. Skip passing $timestamp if you want current time
echo (int)date('i',$timestamp);

Respondido el 31 de enero de 20 a las 08:01

No need to add time() allí. - alex

@alex, I was actually trying to show where he could add his own timestamp - Starx

alex is right... date( 'format_string' ) asume time() as the second argument - aequalsb

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');

Esto generará lo siguiente:


Respondido 11 Oct 13, 19:10

Awesome! Even better then @ScottA solution. This is my code (also with hours) print ltrim(gmdate("H\hi\ms\s", $duration), '0mh'); - Jasom Dotnet

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.

Respondido 26 Feb 14, 09:02

Mi solución:

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

Esto dará como resultado:

0 seconds:            -
10 seconds:        0:10
90 seconds:        1:30
301 seconds:       5:01
1804 seconds:     30:04
3601 seconds:   1:00:01

Respondido el 06 de enero de 17 a las 13:01

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

select f1, ..., date_format( fldTime , '%i' ) * 1  as myTime, ..., ...

respondido 02 mar '12, 18:03

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

$current_date = Date("n-j-Y");
echo $current_date;

// Result m-d-yy


Respondido el 10 de Septiembre de 12 a las 15:09

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/>'; ?>

Respondido 04 Jul 17, 00:07

~12 function calls to remove leading zeros. Your function could be broken down into this (int) "001" - naT erraT

Solo usa esto:

(int) date('i');

Respondido 03 Jul 18, 14:07

@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

use PHP's absolute value function:

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

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

contestado el 01 de mayo de 19 a las 22:05

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

respondido 16 mar '18, 08:03

OP wants minutes WITHOUT leading zeros - aequalsb

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.