PHP date(): minutes without leading zeros

给你一囗甜甜゛ 提交于 2019-12-29 03:33:06

问题


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?


回答1:


Use:

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



回答2:


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:


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



回答4:


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.




回答5:


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




回答6:


This also works

$timestamp = time(); //Or Your timestamp
echo (int)date('i',$timestamp);



回答7:


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



回答8:


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.




回答9:


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



回答10:


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

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



回答11:


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

// Result m-d-yy

9-10-2012



回答12:


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



回答13:


Just use this:

(int) date('i');



回答14:


use PHP's absolute value function:

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

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




回答15:


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



来源:https://stackoverflow.com/questions/4639047/php-date-minutes-without-leading-zeros

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!