diff options
author | Stefan Giehl <stefan@matomo.org> | 2020-12-10 05:18:07 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-10 05:18:07 +0300 |
commit | 7cde69f763f2cbf2b9a27c8a7dcf7bcaa950c3bd (patch) | |
tree | da719a778d96e167ee4ba55c89a529c1c3bb0dce /core/Date.php | |
parent | 06918eea4d2ca01298200348b40f57817433b931 (diff) |
Allow using last (week|month|year) as date param (#16830)
* Allow using last (week|month|year) as date param
* Adds some unit tests
* improve tests
* Make it possible to use last (week|month|year) for api requests
* improve test
Diffstat (limited to 'core/Date.php')
-rw-r--r-- | core/Date.php | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/core/Date.php b/core/Date.php index 4b67eeb9cb..328467ab67 100644 --- a/core/Date.php +++ b/core/Date.php @@ -135,6 +135,12 @@ class Date $date = self::yesterday(); } elseif ($dateString === 'yesterdaySameTime') { $date = self::yesterdaySameTime(); + } else if (preg_match('/last[ -]?week/i', urldecode($dateString))) { + $date = self::lastWeek(); + } else if (preg_match('/last[ -]?month/i', urldecode($dateString))) { + $date = self::lastMonth(); + } else if (preg_match('/last[ -]?year/i', urldecode($dateString))) { + $date = self::lastYear(); } elseif (!is_int($dateString) && ( // strtotime returns the timestamp for April 1st for a date like 2011-04-01,today @@ -187,6 +193,12 @@ class Date return self::yesterdayInTimezone($timezone); } else if ($dateString === 'yesterdaySameTime') { return self::yesterdaySameTimeInTimezone($timezone); + } else if (preg_match('/last[ -]?week/i', urldecode($dateString))) { + return self::lastWeekInTimezone($timezone); + } else if (preg_match('/last[ -]?month/i', urldecode($dateString))) { + return self::lastMonthInTimezone($timezone); + } else if (preg_match('/last[ -]?year/i', urldecode($dateString))) { + return self::lastYearInTimezone($timezone); } else { throw new \Exception("Date::factoryInTimezone() should not be used with $dateString."); } @@ -214,6 +226,21 @@ class Date return self::nowInTimezone($timezone)->subDay(1); } + private static function lastWeekInTimezone($timezone) + { + return new Date(strtotime('-1week', self::todayInTimezone($timezone)->getTimestamp())); + } + + private static function lastMonthInTimezone($timezone) + { + return new Date(strtotime('-1month', self::todayInTimezone($timezone)->getTimestamp())); + } + + private static function lastYearInTimezone($timezone) + { + return new Date(strtotime('-1year', self::todayInTimezone($timezone)->getTimestamp())); + } + /** * Returns the current timestamp as a string with the following format: `'YYYY-MM-DD HH:MM:SS'`. * @@ -586,7 +613,37 @@ class Date */ public static function yesterdaySameTime() { - return new Date(strtotime("yesterday " . date('H:i:s'), self::getNowTimestamp())); + return new Date(strtotime("yesterday " . date('H:i:s', self::getNowTimestamp()), self::getNowTimestamp())); + } + + /** + * Returns a date object set to the day a week ago at midnight in UTC. + * + * @return \Piwik\Date + */ + public static function lastWeek() + { + return new Date(strtotime("-1week 00:00:00", self::getNowTimestamp())); + } + + /** + * Returns a date object set to the day a month ago at midnight in UTC. + * + * @return \Piwik\Date + */ + public static function lastMonth() + { + return new Date(strtotime("-1month 00:00:00", self::getNowTimestamp())); + } + + /** + * Returns a date object set to the day a year ago at midnight in UTC. + * + * @return \Piwik\Date + */ + public static function lastYear() + { + return new Date(strtotime("-1year 00:00:00", self::getNowTimestamp())); } /** |