Hướng dẫn dateinterval to int php
I'm using this code:
I want to calculate, if someone is late to pay then they will be fined per month. And the error message is:
asked Jun 25, 2016 at 13:44 2 The error message appears because
You can get the integer value of months like this
Check the above result in PHP Sandbox So basically your code will look like
answered Jun 25, 2016 at 13:51 1 You calculate the difference in months, but you never actually use that value. The Try it like this:
answered Jun 25, 2016 at 13:48
PevaraPevara 14.1k1 gold badge32 silver badges47 bronze badges 3 Peter Darmis's answer is wrong. I can't downvote nor comment it so I will add this new answer. DateInterval represents a period of time by deconstructing the interval in different parts, years, months, days etc... So "m" in the proposed solution will never be bigger than 12. So you should probably do something like this:
Check it in the sandbox: http://sandbox.onlinephpfunctions.com/code/907b39ffee4586c4c9481ff3e0ea1aeb2d27c7b8 answered Sep 13, 2019 at 14:35
iblancoiblanco 411 silver badge6 bronze badges (PHP 5 >= 5.3.0, PHP 7, PHP 8) DateInterval::__construct — Creates a new DateInterval object Descriptionpublic DateInterval::__construct(string Parametersduration
An interval specification. The format starts with the letter duration Period Designators
Here are some simple examples. Two days is
The specification can
also be represented as a date time. A sample of one year and four days would be These formats are based on the » ISO 8601 duration specification. Errors/Exceptions Throws an
Exception when the Changelog
ExamplesExample #1 Constructing and using DateInterval objects
The above example will output: Example #2 DateInterval example
= new DateInterval('P1W2D'); Output of the above example in PHP 8.2: object(DateInterval)#1 (10) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["invert"]=> int(0) ["days"]=> bool(false) ["from_string"]=> bool(false) } Output of the above example in PHP 8: object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(9) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } Output of the above example in PHP 7: object(DateInterval)#1 (16) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(2) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> bool(false) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } See Also
owen at beliefs.com ¶ 9 years ago
buvinghausen at gmail dot com ¶ 10 years ago
Hernanibus ¶ 4 years ago
kevinpeno at gmail dot com ¶ 11 years ago
daniellehr at gmx dot de ¶ 10 years ago
kuzb ¶ 11 years ago
jawzx01 at gmail dot com ¶ 10 years ago
admin at torntech dot com ¶ 7 years ago
userexamplecom at mailinator dot com ¶ 6 years ago
lsloan-php dot net at umich dot edu ¶ 6 years ago
Anonymous ¶ 1 year ago
= (new DateTime('now'))->add(new DateInterval("PT1H"));var_dump($plusOneHour);?>
grzeniufication ¶ 2 years ago
DateInterval extends \DateInterval sloanlance+php.net gmail com ¶ 4 years ago
Ray.Paseur sometimes uses Gmail ¶ 6 years ago
|