{"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\n

Resulting 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\n

The 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\n

This 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":"\nExporting Records with Timezone Adjustments | Alex Rusin Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Exporting Records with Timezone Adjustments | Alex Rusin Blog\" \/>\n<meta property=\"og:description\" content=\"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...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/\" \/>\n<meta property=\"og:site_name\" content=\"Alex Rusin Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-27T18:35:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-27T18:40:05+00:00\" \/>\n<meta name=\"author\" content=\"alexrusin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"alexrusin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/\"},\"author\":{\"name\":\"alexrusin\",\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\"},\"headline\":\"Exporting Records with Timezone Adjustments\",\"datePublished\":\"2020-08-27T18:35:58+00:00\",\"dateModified\":\"2020-08-27T18:40:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/\"},\"wordCount\":131,\"publisher\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\"},\"articleSection\":[\"Laravel\",\"PHP MySQL Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/\",\"url\":\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/\",\"name\":\"Exporting Records with Timezone Adjustments | Alex Rusin Blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#website\"},\"datePublished\":\"2020-08-27T18:35:58+00:00\",\"dateModified\":\"2020-08-27T18:40:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.alexrusin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Exporting Records with Timezone Adjustments\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.alexrusin.com\/#website\",\"url\":\"https:\/\/blog.alexrusin.com\/\",\"name\":\"Alex Rusin\",\"description\":\"Web Development Blog\",\"publisher\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.alexrusin.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a\",\"name\":\"alexrusin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g\",\"caption\":\"alexrusin\"},\"logo\":{\"@id\":\"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Exporting Records with Timezone Adjustments | Alex Rusin Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/","og_locale":"en_US","og_type":"article","og_title":"Exporting Records with Timezone Adjustments | Alex Rusin Blog","og_description":"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...","og_url":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/","og_site_name":"Alex Rusin Blog","article_published_time":"2020-08-27T18:35:58+00:00","article_modified_time":"2020-08-27T18:40:05+00:00","author":"alexrusin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"alexrusin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/#article","isPartOf":{"@id":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/"},"author":{"name":"alexrusin","@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a"},"headline":"Exporting Records with Timezone Adjustments","datePublished":"2020-08-27T18:35:58+00:00","dateModified":"2020-08-27T18:40:05+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/"},"wordCount":131,"publisher":{"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a"},"articleSection":["Laravel","PHP MySQL Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/","url":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/","name":"Exporting Records with Timezone Adjustments | Alex Rusin Blog","isPartOf":{"@id":"https:\/\/blog.alexrusin.com\/#website"},"datePublished":"2020-08-27T18:35:58+00:00","dateModified":"2020-08-27T18:40:05+00:00","breadcrumb":{"@id":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alexrusin.com\/exporting-records-with-timezone-adjustments\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alexrusin.com\/"},{"@type":"ListItem","position":2,"name":"Exporting Records with Timezone Adjustments"}]},{"@type":"WebSite","@id":"https:\/\/blog.alexrusin.com\/#website","url":"https:\/\/blog.alexrusin.com\/","name":"Alex Rusin","description":"Web Development Blog","publisher":{"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alexrusin.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/a9005ca622862109b2c514050fbaaf9a","name":"alexrusin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c36ef231f9e0b11371891eb84360f4bc?s=96&d=mm&r=g","caption":"alexrusin"},"logo":{"@id":"https:\/\/blog.alexrusin.com\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts\/14326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/comments?post=14326"}],"version-history":[{"count":3,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts\/14326\/revisions"}],"predecessor-version":[{"id":14329,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/posts\/14326\/revisions\/14329"}],"wp:attachment":[{"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/media?parent=14326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/categories?post=14326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alexrusin.com\/wp-json\/wp\/v2\/tags?post=14326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}