Skip to content

Commit a2d0a6a

Browse files
authored
Merge pull request #133 from DenverCoder1/date-format
2 parents 01c5848 + cf708f2 commit a2d0a6a

File tree

5 files changed

+192
-13
lines changed

5 files changed

+192
-13
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,30 @@ If the `theme` parameter is specified, any color customizations specified will b
7373
| `currStreakLabel` | Current streak label | **hex code** without `#` or **css color** |
7474
| `sideLabels` | Total and longest streak labels | **hex code** without `#` or **css color** |
7575
| `dates` | Date range text color | **hex code** without `#` or **css color** |
76+
| `date_format` | Date format (Default: `M j[, Y]`) | See note below on [Date Formats](#date-formats) |
7677
| `type` | Output format (Default: `svg`) | Current options: `svg` or `json` |
7778

79+
### Date Formats
80+
81+
A custom date format can be specified by passing a string to the `date_format` parameter.
82+
83+
The required format is to use format string characters from [PHP's date function](https://www.php.net/manual/en/datetime.format.php) with brackets around the part representing the year.
84+
85+
When the contribution year is equal to the current year, the characters in brackets will be omitted.
86+
87+
**Examples:**
88+
89+
| Date Format | Result |
90+
| :-----------------: | :-----------------------------------------------------------------------------: |
91+
| <pre>d F[, Y]</pre> | <pre>"2020-04-14" => "14 April, 2020"<br/><br/>"2021-04-14" => "14 April"</pre> |
92+
| <pre>j/n/Y</pre> | <pre>"2020-04-14" => "14/4/2020"<br/><br/>"2021-04-14" => "14/4/2021"</pre> |
93+
| <pre>[Y.]n.j</pre> | <pre>"2020-04-14" => "2020.4.14"<br/><br/>"2021-04-14" => "4.14"</pre> |
94+
| <pre>M j[, Y]</pre> | <pre>"2020-04-14" => "Apr 14, 2020"<br/><br/>"2021-04-14" => "Apr 14"</pre> |
95+
7896
### Example
7997

8098
```md
81-
[![GitHub Streak](https://github-readme-streak-stats.herokuapp.com/?user=denvercoder1&currStreakNum=2FD3EB&fire=pink&sideLabels=F00)](https://git.io/streak-stats)
99+
[![GitHub Streak](https://github-readme-streak-stats.herokuapp.com/?user=denvercoder1&currStreakNum=2FD3EB&fire=pink&sideLabels=F00&date_format=[Y.]n.j)](https://git.io/streak-stats)
82100
```
83101

84102
## ℹ️ How these stats are calculated

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: 21 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,26 @@ 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
}
184+
185+
/**
186+
* Test date formatter no brackets different year
187+
*/
188+
public function testDateFormatNoBracketsDiffYear(): void
189+
{
190+
$formatted = formatDate("2000-04-12", "Y/m/d");
191+
$this->assertEquals("2000/04/12", $formatted);
192+
}
193+
194+
/**
195+
* Test date formatter no brackets same year
196+
*/
197+
public function testDateFormatNoBracketsSameYear(): void
198+
{
199+
$year = date("Y");
200+
$formatted = formatDate("$year-04-12", "Y/m/d");
201+
$this->assertEquals("$year/04/12", $formatted);
202+
}
184203
}

tests/RenderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ public function testErrorCardRender(): void
5656
$this->assertEquals($expected, $render);
5757
}
5858

59+
/**
60+
* Test date_format parameter in render
61+
*/
62+
public function testDateFormatRender(): void
63+
{
64+
$year = date("Y");
65+
$this->testStats["currentStreak"]["end"] = "$year-04-12";
66+
$this->testParams["date_format"] = "[Y-]m-d";
67+
// Check that the card is rendered as expected
68+
$render = generateCard($this->testStats, $this->testParams);
69+
$expected = file_get_contents("tests/expected/test_date_card.svg");
70+
$this->assertEquals($expected, $render);
71+
}
72+
5973
/**
6074
* Test JSON render
6175
*/

tests/expected/test_date_card.svg

Lines changed: 121 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)