|
4 | 4 | * Convert date from Y-M-D to more human-readable format
|
5 | 5 | *
|
6 | 6 | * @param string $dateString String in Y-M-D format
|
7 |
| - * @return string formatted Date string |
| 7 | + * @param string $format Date format to use |
| 8 | + * @return string Formatted Date string |
8 | 9 | */
|
9 |
| -function formatDate(string $dateString): string |
| 10 | +function formatDate(string $dateString, string $format): string |
10 | 11 | {
|
11 | 12 | $date = new DateTime($dateString);
|
12 | 13 | // if current year, display only month and day
|
13 | 14 | if (date_format($date, "Y") == date("Y")) {
|
14 |
| - return date_format($date, "M j"); |
| 15 | + // remove brackets and all text within them |
| 16 | + return date_format($date, preg_replace("/\[.*?\]/", "", $format)); |
15 | 17 | }
|
16 |
| - // otherwise, display month, day, and year |
17 |
| - return date_format($date, "M j, Y"); |
| 18 | + // otherwise, display month, day, and year (just brackets removed) |
| 19 | + return date_format($date, str_replace(array("[", "]"), "", $format)); |
18 | 20 | }
|
19 | 21 |
|
20 | 22 | /**
|
@@ -87,24 +89,29 @@ function generateCard(array $stats, array $params = null): string
|
87 | 89 | // get requested theme, use $_REQUEST if no params array specified
|
88 | 90 | $theme = getRequestedTheme($params ?? $_REQUEST);
|
89 | 91 |
|
| 92 | + // get date format |
| 93 | + $dateFormat = isset(($params ?? $_REQUEST)["date_format"]) |
| 94 | + ? ($params ?? $_REQUEST)["date_format"] |
| 95 | + : "M j[, Y]"; |
| 96 | + |
90 | 97 | // total contributions
|
91 | 98 | $totalContributions = $stats["totalContributions"];
|
92 |
| - $firstContribution = formatDate($stats["firstContribution"]); |
| 99 | + $firstContribution = formatDate($stats["firstContribution"], $dateFormat); |
93 | 100 | $totalContributionsRange = $firstContribution . " - Present";
|
94 | 101 |
|
95 | 102 | // current streak
|
96 | 103 | $currentStreak = $stats["currentStreak"]["length"];
|
97 |
| - $currentStreakStart = formatDate($stats["currentStreak"]["start"]); |
98 |
| - $currentStreakEnd = formatDate($stats["currentStreak"]["end"]); |
| 104 | + $currentStreakStart = formatDate($stats["currentStreak"]["start"], $dateFormat); |
| 105 | + $currentStreakEnd = formatDate($stats["currentStreak"]["end"], $dateFormat); |
99 | 106 | $currentStreakRange = $currentStreakStart;
|
100 | 107 | if ($currentStreakStart != $currentStreakEnd) {
|
101 | 108 | $currentStreakRange .= " - " . $currentStreakEnd;
|
102 | 109 | }
|
103 | 110 |
|
104 | 111 | // longest streak
|
105 | 112 | $longestStreak = $stats["longestStreak"]["length"];
|
106 |
| - $longestStreakStart = formatDate($stats["longestStreak"]["start"]); |
107 |
| - $longestStreakEnd = formatDate($stats["longestStreak"]["end"]); |
| 113 | + $longestStreakStart = formatDate($stats["longestStreak"]["start"], $dateFormat); |
| 114 | + $longestStreakEnd = formatDate($stats["longestStreak"]["end"], $dateFormat); |
108 | 115 | $longestStreakRange = $longestStreakStart;
|
109 | 116 | if ($longestStreakStart != $longestStreakEnd) {
|
110 | 117 | $longestStreakRange .= " - " . $longestStreakEnd;
|
|
0 commit comments