-
-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Rector Rule] Replace date
, strtotime
, and time
calls with Carbon
equivalents
#6749
Merged
samsonasik
merged 12 commits into
rectorphp:main
from
gollumeo:feat/replace-strtotime-with-carbon
Mar 4, 2025
Merged
[Rector Rule] Replace date
, strtotime
, and time
calls with Carbon
equivalents
#6749
samsonasik
merged 12 commits into
rectorphp:main
from
gollumeo:feat/replace-strtotime-with-carbon
Mar 4, 2025
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…space in the fixture
- Add DateFuncCallToCarbonRector to replace date(), strtotime(), and time() - patterns - Handle parent Minus node for time() - (N) or time() - (60 * 60 * 24 * N) - Fix detectTimeUnit() to support both LNumber and Mul - Reverse TIME_UNITS order to detect subWeeks() before subSeconds() - Update fixtures to cover seconds, minutes, hours, days, and weeks
…om/gollumeo/rector-src into feat/replace-strtotime-with-carbon
samsonasik
reviewed
Feb 22, 2025
/cc @melbings @kylekatarnls could you verify this change? Thank you. |
kylekatarnls
approved these changes
Mar 3, 2025
Thank you @kylekatarnls @gollumeo , let's give it a try 👍 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related issue
Closes #8695
Description
This PR adds a Rector rule to replace certain native PHP date/time function calls with Carbon equivalents. The conversions are based on the examples provided in the original issue
Implemented changes
date('format', strotime(...))
→Carbon::parse(...)->format('format')
strtotime($date)
→Carbon::parse($date)->timestamp
time() - (60 * 60 * 24 * 14)
→Carbon::now()->subWeeks(2)->timestamp
Code examples
Before
After
Tests & validation
strtotime.php.inc
date_with_strtotime.php.inc
time_calculation.php.inc
simple_date.php.inc
Notes
date('format', strtotime(...))
and applies the correct Carbon replacementseconds
,minutes
,hours
,days
,weeks
)Looking forward to any suggestions or improvements! Let me know if there's anything else I should adjust 😁