Last active
January 13, 2024 18:13
-
-
Save d3vCr0w/6d318363b3701ed89a28e354eccda392 to your computer and use it in GitHub Desktop.
Get last 12 month names from a specific month or date with Laravel - Carbon
This file contains hidden or 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
<?php | |
use Carbon\Carbon; | |
//your class starts here | |
public function getLastTwelveMonthNames($date){ | |
Carbon::setlocale(config('app.locale')); | |
$month_names = [ | |
ucfirst((new Carbon($date))->translatedFormat('F')) | |
]; | |
for($i=1;$i<=11;$i++){ | |
$month_names[$i] = ucfirst((new Carbon($date))->subMonth()->translatedFormat('F')); | |
$date = (new Carbon($date))->subMonth(); | |
} | |
return $month_names; | |
} | |
//your class ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Returns an array (obviously I did this in Jan, so last 12 is correct at this time):
[ "January", "December", "November", "October", "September", "August", "July", "June", "May", "April", "March", "February" ]