DEV Community

Discussion on: What are the great function/method/etc. names in popular libs/languages?

Collapse
 
kip13 profile image
kip

A common problem in PHP (maybe in some other langs too) is the way to move between dates and get the incorrect value, for example when you decrease or increase a month(s):

>>> (new \DateTime('2017-01-31'))->add(new \DateInterval('P1M'))->format('Y-m-d')
=> "2017-03-03"

Carbon a good library offers some methods to avoid this behaivor and their names are really good:

>>> (new Carbon('2017-01-31'))->addMonthNoOverflow()->format('Y-m-d')
=> "2017-02-28"

If you wanna get the calculation with this behaivor, no problem:

>>> (new Carbon('2017-01-31'))->addMonthWithOverflow()->format('Y-m-d')
=> "2017-03-03"

Other methods are: createMidnightDate, localeHasDiffOneDayWords, createSafe...