Coder Social home page Coder Social logo

Comments (6)

tassoevan avatar tassoevan commented on May 28, 2024

+1 That would be nice.

from carbon.

BradEstey avatar BradEstey commented on May 28, 2024

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.

tassoevan avatar tassoevan commented on May 28, 2024

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.

briannesbitt avatar briannesbitt commented on May 28, 2024

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.

briannesbitt avatar briannesbitt commented on May 28, 2024

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.

BradEstey avatar BradEstey commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.