{"id":14326,"date":"2020-08-27T18:35:58","date_gmt":"2020-08-27T18:35:58","guid":{"rendered":"https:\/\/alexrusin.com\/?p=14326"},"modified":"2020-08-27T18:40:05","modified_gmt":"2020-08-27T18:40:05","slug":"exporting-records-with-timezone-adjustments","status":"publish","type":"post","link":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/","title":{"rendered":"Exporting Records with Timezone Adjustments"},"content":{"rendered":"\n
As a rule, dates in a database are stored in UTC. So, when you export records from database constrained by dates, it is a good idea to use appropriate timezone in export query. You can do it in one of two ways. One way is create incoming dates in user’s timezone, then covert time to UTC and query the database. Another way is to convert database column (created_at) to the users timezone and query against it. <\/p>\n\n\n\n
The first approach. First we need to convert time coming to us from the user into the UTC.<\/p>\n\n\n\n
$startAtUtc = (new Carbon($this->request->input('start_at'), $this->timezone))->startOfDay()->setTimezone('UTC');\n $endAtUtc = (new Carbon($this->request->input('end_at'), $this->timezone))->startOfDay()->setTimezone('UTC');\n $query = Orders::whereBetween('created_at', [$startAtUtc, $endAtUtc]);<\/pre>\n\n\n\nResulting query will look like so:<\/p>\n\n\n\n
Start at UTC: 2020-07-14 07:00:00\nEnd at UTC: 2020-08-27 07:00:00\nExport query: select * from `orders` where `created_at` between ? and ?<\/pre>\n\n\n\nThe second approach. First we need to get timezone offset and then we do the query.<\/p>\n\n\n\n
$timezoneOffset = Carbon::now()->setTimezone($this->timezone)->format('P');\n\n $query = Orders::whereRaw(\"convert_tz(created_at, '+00:00','\" . $timezoneOffset . \"') between '\" . (new Carbon($this->request->input('start_at'), $this->timezone))->startOfDay() . \"' and '\" . (new Carbon($this->request->input('end_at'), $this->timezone))->startOfDay() . \"'\"\n );\n $query->toSql();<\/pre>\n\n\n\nThis is how the query will look like:<\/p>\n\n\n\n
select * from `orders` where convert_tz(created_at, '+00:00','-07:00') between '2020-07-27 00:00:00' and '2020-08-27 00:00:00' <\/pre>\n\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"
As a rule, dates in a database are stored in UTC. So, when you export records from database constrained by dates, it is a good idea to use appropriate timezone in export query. You can do it in one of two ways. One way is create incoming dates in user’s timezone, then covert time to…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[12,10],"tags":[],"class_list":["post-14326","post","type-post","status-publish","format-standard","hentry","category-laravel","category-php-mysql-development"],"yoast_head":"\n
Exporting Records with Timezone Adjustments | Alex Rusin Blog<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n