[Rector Rule] Replace date, strtotime, and time calls with Carbon equivalents#6749
Conversation
…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
…ng args Co-authored-by: Samson Asik <samsonasik@gmail.com>
|
/cc @melbings @kylekatarnls could you verify this change? Thank you. |
|
Thank you @kylekatarnls @gollumeo , let's give it a try 👍 |
|
We're seeing an issue at times with this – Before date('Y', $pricing->timestamp) . ')After Carbon::now()->format('Y')
For this reason, we don't feel comfortable enabling this going forward, as it would break functionality. But if this can be addressed, it's a great feature! |
|
I don't get the case: if |
|
This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work. |
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)->timestamptime() - (60 * 60 * 24 * 14)→Carbon::now()->subWeeks(2)->timestampCode examples
Before
After
Tests & validation
strtotime.php.incdate_with_strtotime.php.inctime_calculation.php.incsimple_date.php.incNotes
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 😁