From d13c37318367ffa43acc3ee167783481c3876a17 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Wed, 26 Feb 2014 17:58:19 +1100 Subject: [PATCH] Add date_diff fallback for PHP < 5.3 --- lib/fallbacks/date-diff.php | 99 +++++++++++++++++++++++++++++++++++++ readme.txt | 7 ++- sportspress.php | 1 + 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 lib/fallbacks/date-diff.php diff --git a/lib/fallbacks/date-diff.php b/lib/fallbacks/date-diff.php new file mode 100644 index 00000000..9c7c0a99 --- /dev/null +++ b/lib/fallbacks/date-diff.php @@ -0,0 +1,99 @@ + + * + */ + +if(!function_exists('date_diff')) { + class DateInterval { + public $y; + public $m; + public $d; + public $h; + public $i; + public $s; + public $invert; + public $days; + + public function format($format) { + $format = str_replace('%R%y', + ($this->invert ? '-' : '+') . $this->y, $format); + $format = str_replace('%R%m', + ($this->invert ? '-' : '+') . $this->m, $format); + $format = str_replace('%R%d', + ($this->invert ? '-' : '+') . $this->d, $format); + $format = str_replace('%R%h', + ($this->invert ? '-' : '+') . $this->h, $format); + $format = str_replace('%R%i', + ($this->invert ? '-' : '+') . $this->i, $format); + $format = str_replace('%R%s', + ($this->invert ? '-' : '+') . $this->s, $format); + + $format = str_replace('%y', $this->y, $format); + $format = str_replace('%m', $this->m, $format); + $format = str_replace('%d', $this->d, $format); + $format = str_replace('%h', $this->h, $format); + $format = str_replace('%i', $this->i, $format); + $format = str_replace('%s', $this->s, $format); + + return $format; + } + } + + function date_diff(DateTime $date1, DateTime $date2) { + + $diff = new DateInterval(); + + if($date1 > $date2) { + $tmp = $date1; + $date1 = $date2; + $date2 = $tmp; + $diff->invert = 1; + } else { + $diff->invert = 0; + } + + $diff->y = ((int) $date2->format('Y')) - ((int) $date1->format('Y')); + $diff->m = ((int) $date2->format('n')) - ((int) $date1->format('n')); + if($diff->m < 0) { + $diff->y -= 1; + $diff->m = $diff->m + 12; + } + $diff->d = ((int) $date2->format('j')) - ((int) $date1->format('j')); + if($diff->d < 0) { + $diff->m -= 1; + $diff->d = $diff->d + ((int) $date1->format('t')); + } + $diff->h = ((int) $date2->format('G')) - ((int) $date1->format('G')); + if($diff->h < 0) { + $diff->d -= 1; + $diff->h = $diff->h + 24; + } + $diff->i = ((int) $date2->format('i')) - ((int) $date1->format('i')); + if($diff->i < 0) { + $diff->h -= 1; + $diff->i = $diff->i + 60; + } + $diff->s = ((int) $date2->format('s')) - ((int) $date1->format('s')); + if($diff->s < 0) { + $diff->i -= 1; + $diff->s = $diff->s + 60; + } + + $start_ts = $date1->format('U'); + $end_ts = $date2->format('U'); + $days = $end_ts - $start_ts; + $diff->days = round($days / 86400); + + if (($diff->h > 0 || $diff->i > 0 || $diff->s > 0)) + $diff->days += ((bool) $diff->invert) + ? 1 + : -1; + + return $diff; + + } + +} diff --git a/readme.txt b/readme.txt index e59e2423..4d9a3c3e 100644 --- a/readme.txt +++ b/readme.txt @@ -1,5 +1,5 @@ === SportsPress - automated league statistics === -Contributors: themeboy +Contributors: ThemeBoy, aylaview, jenszackrisson Tags: sports, sports journalism, teams, team management, fixtures, results, standings, league tables, leagues, reporting, themeboy, wordpress sports, configurable Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=support@themeboy.com&item_name=Donation+for+SportsPress Requires at least: 3.8 @@ -144,6 +144,11 @@ SportsPress is currently in beta and is undergoing testing. We are still activel == Changelog == += 0.4.1 = +* Tweak - Activate checkbox when all players are added to player list. +* Fix - Function date_diff added for PHP < 5.3. +* Localization - Swedish translation by jenszackrisson. + = 0.4 = * Feature - SportsPress Status dashboard widget added to display number of events and countdown in admin. * Feature - New dashboard menu icons. diff --git a/sportspress.php b/sportspress.php index e345f83e..3df21df2 100644 --- a/sportspress.php +++ b/sportspress.php @@ -24,6 +24,7 @@ define( 'SPORTSPRESS_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); define( 'SPORTSPRESS_PLUGIN_FILE', __FILE__ ); // Libraries +require_once dirname( __FILE__ ) . '/lib/fallbacks/date-diff.php' ; require_once dirname( __FILE__ ) . '/lib/eos/eos.class.php' ; // Globals