PHP date(): minutes without leading zeros

后端 未结 15 1500
粉色の甜心
粉色の甜心 2020-12-13 23:17

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 manuall

相关标签:
15条回答
  • 2020-12-13 23:44

    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 讨论(0)
  • 2020-12-13 23:46

    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
    
    0 讨论(0)
  • 2020-12-13 23:47

    Use:

    $minutes = intval(date('i'));
    
    0 讨论(0)
  • 2020-12-13 23:48

    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' );
    
    0 讨论(0)
  • 2020-12-13 23:49

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

    select f1, ..., date_format( fldTime , '%i' ) * 1  as myTime, ..., ...
    
    0 讨论(0)
  • 2020-12-13 23:50

    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.

    0 讨论(0)
提交回复
热议问题