![]() "D")) Ĭontributed By: Anthony If you're using PHP >= 5.5, instead of using "glavic at gmail dot com"'s DateTimeEnhanced class, use the built in DateTimeImmutable type. Substract days to go back to the last day of previous month. Check if the day is changed, if so we skipped to the next month. (Only a side note on procedural style mentions it, but it obviously does not apply to object oriented style.)Ĭontributed By: Angelo Another simple solution to adding a month but not autocorrecting days to the next month is this. You could misunderstand it that the method would return a new instance with the modified value, but in fact it modifies itself! This is undocumented here. Beware when adding months add($interval) Ĭontributed By: Anonymous Note that the add() and sub() methods will modify the value of the object you're calling the method on! This is very untypical for a method that returns a value of its own type. $date->add(new DateInterval('P7Y5M4DT4H3M2S')) Further DateTime::add() examples add(new DateInterval('PT10H30S')) Įcho $date->format('Y-m-d H:i:s'). DateTime::add() example add(new DateInterval('P10D')) The PHP DateTime::add() method returns the DateTime object for method chaining or FALSE on failure. Object - Procedural style only: A DateTime object returned by date_create(). PHP DateTime::add() Syntax public DateTime::add ( DateInterval $interval ) : DateTime date_add ( DateTime $object, DateInterval $interval ) : DateTime PHP DateTime::add() Parameters The PHP DateTime:: add() method adds an amount of days, months, years, hours, minutes and seconds to a DateTime object. PHP DateTime::add() Method What does DateTime::add() do?
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |