PHP date(): minutes without leading zeros
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?
$minutes = intval(date('i'));
PHP date(): minutes without leading zeros – Blog – Nabin Nepal , Format a local date and time and return the formatted date strings: n - A numeric representation of a month, without leading zeros (1 to 12); t - The + 0100); P - Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.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.
For times with more information than just minutes:
ltrim() - Strip whitespace (or other characters) from the beginning of a string
PHP date() Function, Returns a new DateTime object representing the date and time specified by the G and H, 24-hour format of an hour with or without leading zeros, 0 through information found in the string to parse, and sets the hour, minute and second to 0. for creating DateTime objects when used with DateTime::createFromFormat (): i - Minutes with leading zeros (00 to 59) s - Seconds, with leading zeros (00 to 59) u - Microseconds (added in PHP 5.2.2) e - The timezone identifier (Examples: UTC, GMT, Atlantic/Azores) I (capital i) - Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
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' );
date - Manual, Get code examples like "php date without leading zero" instantly right from your google search results with the Grepper Chrome Extension. Minutes with leading zeros: 00 to 59: s: Seconds with leading zeros: 00 through 59: u: Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds. Example: 654321: v
I tried to find this for seconds as well, gave up the search and just casting the result as a int like this:
That will get rid of the leading zero's in a fast efficient way.
DateTime::createFromFormat - Manual, Anytime you have a question about a particular function in PHP, the easiest way to get quick answers is by visiting php.net, which has great documentation on all of the language's capabilities. Looking up a function is easy, just visit http://php.net/<function name> and it will forward you to the
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
php date without leading zero Code Example, For example using a 24 hour notation without leading zeros is the option '%G' in PHP but '%k' in MySQL. When using dynamically generated date formatting string, be careful to generate the correct options for either PHP or MySQL.
Another short way is to fill the minutes with a leading zero using: String(date.getMinutes()).padStart(2, "0"); Meaning: Make the string two chars long, if a char is missing then set 0 at this position. See docs at str.padStart(targetLength, padString)
Given a number in string format and the task is to remove all leading zeros from the given string in PHP. Examples: Input : str = 00006665555 Output : 6665555 Input : str = 00775505 Output : 775505
Displays the minute as a number without leading zeros (for example, 12:1:15). Use %m if this is the only character in your user-defined numeric format. mm : Displays the minute as a number with leading zeros (for example, 12:01:15). s : Displays the second as a number without leading zeros (for example, 12:15:5).
- May I ask WHY the hell there is no minutes without zeros ???
- Yes please ask .. i want to hear that answer too ...
- see my answer below...
abs( date( 'i' ) )does not have the same issue as
- Crazy PHP behaviour! It treats the int
09as an octal number because of the leading zero - which will evaluate to
9isn't a valid octal number. But it treats the string
09as the decimal integer
9. Good bye expected behaviour! :)
- It's better to use
intval(). It's supposed to be much faster.
- Well, according to documentation, this is an expected behaviour due to second parameter of intval which is
baseand by default it is equal to 10. PHP will treat input an octal number if base explicitly defined as
- @hek2mgl see my answer below...
abs( date( 'i' ) )does not have the same issue as
echo +'05';results in printing the value
+$date->format("your-format")will return an integer :)
- this doesn't work on times less then 1 minute.. for example: 00:24 would result in :24