-
Notifications
You must be signed in to change notification settings - Fork 51
/
DateHelper.php
44 lines (39 loc) · 1.07 KB
/
DateHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace App\Helpers;
use Carbon\Carbon;
class DateHelper
{
/**
* Formats date string in 'd/m/Y' format to 'Y-m-d' format for storage.
*
* @param string $date
*
* @return string
*/
public static function formatDateToSave($date)
{
return Carbon::parse(str_replace('/', '-', $date))
->format(config('constants.date_format'));
}
/**
* Get an array of month details. Returns month number, name and year for that month.
*
* @param int $count the number of previous months from today to retrieve
*
* @return array
*/
public static function getPreviousMonths($count)
{
$monthsList = [];
$date = new Carbon('first day of this month');
for ($index = 0; $index <= $count; $index++) {
$monthsList[] = [
'id' => $date->format('m'),
'name' => $date->format('F'),
'year' => $date->format('Y'),
];
$date = $date->subMonth();
}
return $monthsList;
}
}