Comments (1)
Hello, they are both handled by the magic __call
and the unit is normalized so to allow both with or without s
.
In practice they are indeed just alias one for each other. And because the default value is 1
, calling it with no parameters will add 1 unit.
But we recommend to use it semantically (to make the code unambiguous and easier to understand) following the rules below:
- Don't pass any argument to
addMonth()
,addDay()
,addSecond()
etc. - Always pass 1 argument to
addMonths($value)
,addDays($value)
,addSeconds($value)
etc.
(the plural method is meant to pass either a value different than 1 or a dynamic value (variable, function call result, etc.)
I considered already to make the parameter mandatory for plural method and throw an exception when passing argument to the singular method, but this is a breaking change and it has a lot of indirect consequences that can make it hard for all the users to update (most people have carbon as a sub-dependency so they don't control easily which version they have, that's why we need to proceed breaking changes with parsimony).
As a first step (when stepping from 2.x to 3.x) the type has moved from PHPDoc to concrete type hint (previously it was practically possible to pass null
or false
.
For the documentation, here it aims to tell how to use in a way that is relevant and is guaranteed to work on the long run, calling ->addMonth(2)
or ->addMonths()
will likely trigger an deprecation notice, then an error in a later version, so I advise to consider following the documentation even if at the moment it's not enforced at runtime.
from carbon.
Related Issues (20)
- Unknown or bad timezone (:/etc/localtime) HOT 2
- Carbon::createFromFormat Incorrect Month Output for February HOT 1
- Error parsing date HOT 1
- Unserialize from Carbon v2 objects HOT 1
- Carbon createFromTimestampMs no default time zone if it is set in env variable of Laravel app HOT 1
- `CarbonInterval`'s `days` property return false HOT 2
- [Translation tool] sr_RS@latin
- Date difference problem HOT 5
- isSameDay: Too few arguments HOT 1
- Inconsistent Date Calculation When Adding Months Across February HOT 5
- Carbon\Carbon::rawAddUnit(): Argument #3 ($value) must be of type int|float, string given HOT 5
- Failed to parse time dd/mm/yyyy using parseFromLocale with Portuguese HOT 1
- Check month issue HOT 1
- PHP Version < 7.4 broken HOT 2
- Psalm reports MissingDependency on CarbonPeriod HOT 1
- Syntax Supported Only After PHP 7.4 Causes Errors in Versions Prior to 7.4 HOT 1
- Undocumented breaking change: isSameMonth() HOT 3
- CarbonPeriod loses timezone when accessing days
- Using "Depends on the following methods" @method in traits causes PHPStan to lose knowledge of type resulting in errors
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from carbon.