Timestamp.php 828 B

123456789101112131415161718192021222324252627
  1. <?php
  2. namespace App\Models\Traits;
  3. trait Timestamp{
  4. public function getCreatedAtAttribute($rawTime){
  5. return $this->changeTimeZone($rawTime, '',config('app.timezone'));
  6. }
  7. public function getUpdatedAtAttribute($rawTime){
  8. return $this->changeTimeZone($rawTime, '',config('app.timezone'));
  9. }
  10. public function changeTimeZone($dateString, $timeZoneSource = null, $timeZoneTarget = null){
  11. if (empty($timeZoneSource)) {
  12. $timeZoneSource = date_default_timezone_get();
  13. }
  14. if (empty($timeZoneTarget)) {
  15. $timeZoneTarget = date_default_timezone_get();
  16. }
  17. $dt = new \DateTime($dateString, new \DateTimeZone($timeZoneSource));
  18. $dt->setTimezone(new \DateTimeZone($timeZoneTarget));
  19. return $dt->format('Y-m-d H:i:s');
  20. }
  21. }