記錄一下因為時區原因導致線上bug被罰款200RMB的慘痛教訓
阿新 • • 發佈:2020-08-25
因為專案需要寫到中東國家的一天最多獲得的數量
所以在判斷時,我的寫法就是將時間簡單的+5
但是這樣在凌晨就會有問題
發現之後優化為
// 獲取區域對應的時區 public function regionTimeZone($region) { if (!array_key_exists($region, $this->regionTimeZoneMap)) { return $this->defaultTimeZone; } return $this->regionTimeZoneMap[$region]; } // 獲取該時區的當前時間 public function getRegionDateString($region, $format = 'Y-m-d H:i:s') { $date = null; date_default_timezone_set("Etc/GMT-{$this->regionTimeZone($region)}"); $date = date($format); date_default_timezone_set("Etc/GMT-{$this->defaultTimeZone}");return $date; } // 獲取該時區的當前時間戳 public function getRegionDateTimestamp($region) { $timestamp = null; date_default_timezone_set("Etc/GMT-{$this->regionTimeZone($region)}"); $timestamp = time(); date_default_timezone_set("Etc/GMT-{$this->defaultTimeZone}");return $timestamp; } // 獲取該時區的當期時間對應某個區域的時間 public function getRegionConvertTimestamp($sourceRegion, $destRegion, $sourceDate = '') { $date = null; date_default_timezone_set("Etc/GMT-{$this->regionTimeZone($sourceRegion)}"); $sourceRegionTimeZone = $this->regionTimeZone($sourceRegion); $destRegionTimeZone = $this->regionTimeZone($destRegion); if (empty($sourceDate)) { $sourceDate = date('Y-m-d H:i:s'); } if ($sourceRegionTimeZone <= $destRegionTimeZone) { $timestamp = strtotime($sourceDate . ' + ' . ($destRegionTimeZone - $sourceRegionTimeZone) . ' hours'); } else { $timestamp = strtotime($sourceDate . ' - ' . ($sourceRegionTimeZone - $destRegionTimeZone) . ' hours'); } $date = date('Y-m-d H:i:s', $timestamp); date_default_timezone_set("Etc/GMT-{$this->defaultTimeZone}"); return $date; } // 獲取該時區的一天開始日期對應某個區域的開始日期 public function getRegionConvertStartDate($sourceRegion, $destRegion) { $date = null; date_default_timezone_set("Etc/GMT-{$this->regionTimeZone($sourceRegion)}"); $sourceRegionTimeZone = $this->regionTimeZone($sourceRegion); $destRegionTimeZone = $this->regionTimeZone($destRegion); if ($sourceRegionTimeZone <= $destRegionTimeZone) { $timestamp = strtotime(date('Y-m-d') . ' 00:00:00' . ' + ' . ($destRegionTimeZone - $sourceRegionTimeZone) . ' hours'); } else { $timestamp = strtotime(date('Y-m-d') . ' 00:00:00' . ' - ' . ($sourceRegionTimeZone - $destRegionTimeZone) . ' hours'); } $date = date('Y-m-d H:i:s', $timestamp); date_default_timezone_set("Etc/GMT-{$this->defaultTimeZone}"); return $date; } // 獲取該時區的一天結束日期對應某個區域的結束日期 public function getRegionConvertEndDate($sourceRegion, $destRegion) { $date = null; date_default_timezone_set("Etc/GMT-{$this->regionTimeZone($sourceRegion)}"); $sourceRegionTimeZone = $this->regionTimeZone($sourceRegion); $destRegionTimeZone = $this->regionTimeZone($destRegion); if ($sourceRegionTimeZone <= $destRegionTimeZone) { $timestamp = strtotime(date('Y-m-d') . ' 23:59:59' . ' + ' . ($destRegionTimeZone - $sourceRegionTimeZone) . ' hours'); } else { $timestamp = strtotime(date('Y-m-d') . ' 23:59:59' . ' - ' . ($sourceRegionTimeZone - $destRegionTimeZone) . ' hours'); } $date = date('Y-m-d H:i:s', $timestamp); date_default_timezone_set("Etc/GMT-{$this->defaultTimeZone}"); return $date; }
謹記!!!