Skip to content

Commit 52495a9

Browse files
committed
feat: Add date_format parameter
1 parent a47a091 commit 52495a9

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/card.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
* Convert date from Y-M-D to more human-readable format
55
*
66
* @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
89
*/
9-
function formatDate(string $dateString): string
10+
function formatDate(string $dateString, string $format): string
1011
{
1112
$date = new DateTime($dateString);
1213
// if current year, display only month and day
1314
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));
1517
}
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));
1820
}
1921

2022
/**
@@ -87,24 +89,29 @@ function generateCard(array $stats, array $params = null): string
8789
// get requested theme, use $_REQUEST if no params array specified
8890
$theme = getRequestedTheme($params ?? $_REQUEST);
8991

92+
// get date format
93+
$dateFormat = isset(($params ?? $_REQUEST)["date_format"])
94+
? ($params ?? $_REQUEST)["date_format"]
95+
: "M j[, Y]";
96+
9097
// total contributions
9198
$totalContributions = $stats["totalContributions"];
92-
$firstContribution = formatDate($stats["firstContribution"]);
99+
$firstContribution = formatDate($stats["firstContribution"], $dateFormat);
93100
$totalContributionsRange = $firstContribution . " - Present";
94101

95102
// current streak
96103
$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);
99106
$currentStreakRange = $currentStreakStart;
100107
if ($currentStreakStart != $currentStreakEnd) {
101108
$currentStreakRange .= " - " . $currentStreakEnd;
102109
}
103110

104111
// longest streak
105112
$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);
108115
$longestStreakRange = $longestStreakStart;
109116
if ($longestStreakStart != $longestStreakEnd) {
110117
$longestStreakRange .= " - " . $longestStreakEnd;

tests/OptionsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function testHideBorder(): void
169169
public function testDateFormatSameYear(): void
170170
{
171171
$year = date("Y");
172-
$formatted = formatDate("$year-04-12");
172+
$formatted = formatDate("$year-04-12", "M j[, Y]");
173173
$this->assertEquals("Apr 12", $formatted);
174174
}
175175

@@ -178,7 +178,7 @@ public function testDateFormatSameYear(): void
178178
*/
179179
public function testDateFormatDifferentYear(): void
180180
{
181-
$formatted = formatDate("2000-04-12");
181+
$formatted = formatDate("2000-04-12", "M j[, Y]");
182182
$this->assertEquals("Apr 12, 2000", $formatted);
183183
}
184184
}

0 commit comments

Comments
 (0)