Skip to content

Conversation

AndrewMast
Copy link
Contributor

Why

I added isNowOrPast() and isNowOrFuture() in #27. Since you can now basically use <, >, <=, and >= to compare to now, I thought I might as well try adding ==.

What

  • Added the isNow method using $this->equalTo($this->nowWithSameTz());
  • Added tests in both tests/Carbon/IsTest.php and tests/CarbonImmutable/IsTest.php

Note

I don't love my wording of the docblock. Determines if the instance is now. doesn't sound quite right, but Determines if the instance is now, ie. equal to now. doesn't make sense either. Let me know if you want me to change this before merging.

@kylekatarnls
Copy link
Contributor

I don't think it's a good idea, because in real life this method will never return true:

If you don't mock the time:

Carbon::now()->isNow() returns false

Because every time now() is called you can a new datetime that is few micro-seconds later than the previous time you called it.

If you want to do such check in unit tests with frozen time, then you can do:

Carbon::now()->is('now')

So it's already available with almost the same syntax.

@AndrewMast
Copy link
Contributor Author

Understandable. I was not aware of ->is('now'). My idea for this operation would be for logic done right after creating a model, but that works instead.

@AndrewMast AndrewMast deleted the feat/is-now branch April 2, 2025 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants