Comments (6)
+1 That would be nice.
from carbon.
Maybe firstInMonth(), lastInMonth(), nthInMonth(), nextInMonth() would be better syntax, since then it would take the current date and return a value within the boundaries of that month. It also allows for expanding into firstInQuarter() and firstInYear() options.
Carbon::now()->firstInMonth(Carbon::THURSDAY);
// Returns the date of the first Thursday in the current month.
Carbon::now()->lastInMonth(Carbon::THURSDAY);
// Returns the date of the last Thursday in the current month.
Carbon::now()->nthInMonth(Carbon::THURSDAY, 2);
// Returns the date of the 2nd Thursday in the current month.
Carbon::now()->nextInMonth(Carbon::THURSDAY);
// Returns the next Thursday in the current month.
Carbon::now()->next(Carbon::THURSDAY);
// Returns the next Thursday, regardless of month/quarter/year etc..
Carbon::now()->last(Carbon::THURSDAY);
// Returns the date of the last Thursday before the current date.
Carbon::now()->firstInQuarter(Carbon::THURSDAY);
// Returns the first Thursday in the current quarter.
Carbon::now()->lastInQuarter(Carbon::THURSDAY);
// Returns the last Thursday in the current quarter.
Carbon::now()->nthInYear(Carbon::THURSDAY, 10);
// Returns the 10th Thursday in the current year.
Carbon::now()->lastInYear(Carbon::THURSDAY);
// Returns the last Thursday in the current year.
from carbon.
I think that bound nth*() methods in month, year, etc. is not interesting here, since it does not agree with other methods that simply increase a superior date component. So, a code like
Carbon::createFromDate(2013, 6)->nth(Carbon::FRIDAY, 5);
would return a july date, not a invalid value or exception.
from carbon.
I like your 2nd option... firstInMonth() etc. Is it expected to modify the current date or return a new instance? I assume modify the current instance?
Agreed about the nthInMonth(Carbon::FRIDAY, 6)
issue ... what would that return? Maybe it could return a boolean false (indicating an invalid request) and no modification would be done ?!?
Just a heads up that there also is this for creating new dates http://www.php.net/manual/en/datetime.formats.relative.php
from carbon.
Wacky, I left a long comment but don't see it anymore. Argh.
Anyway, I pulled in your changes and then proceeded to modify the code a bit. Its in master, see what you think.
from carbon.
Sweet! I juggled back and forth on whether last() should be previous() as well. I felt like last(Carbon::MONDAY) was more recognizable as "last Monday" when reading.. and shorter. But previous() is more clear that it is in the past and not some cryptic prophecy of when the last Monday will occur. :)
from carbon.
Related Issues (20)
- Carbon::createFromFormat creates a different time when using Carbon::setTestNow than when using system datetime HOT 2
- [Translation tool] ms HOT 3
- [Translation tool] uk
- CarbonImmutable Macro Causes PHPStan Internal Error HOT 7
- `createFromFormat` throw an exception for a format from which `hasFormat` returned `true` HOT 7
- PHT as a valid timezone identifier HOT 1
- (Question) Globally change timezone doesnt work as expected HOT 3
- Carbon support for time-zone-aware doctrine types HOT 3
- Carbon period does not accept intervals smaller than 1 HOT 4
- Create From Date Retuns date in March when Febuary is passed in on the 29th and 30th of the Month HOT 1
- Unexpected date from createFromFormat HOT 4
- Macros are stripped from the Carbon instance when casting to immutable HOT 1
- Cannot set Hour to 0 in daylight savings day, diffInDays broken? HOT 3
- Feature suggestion: endOfDay() alias HOT 2
- Translation tool: February missing, March listed twice HOT 1
- CarbonImmutable ignores second parameter (Timezone) HOT 1
- UnknownGetterException on serialization HOT 1
- CarbonPeriodImmutable::instance leads to 'Invalid constructor parameters' Exception in Version >= 2.68 HOT 1
- Feature: add `weekdays` support to CarbonInterval HOT 1
- CarbonTypeConverter broken with Doctrine/Dbal 3.7 HOT 3
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.