John Davidson

laravel - Question about timezone when using PHP and Carbon

0 comments
Message:


In my Laravel application I have a form in which you put the date to capture the date of an expense. This field is a MySQL DATE field so the format is yyyy-mm-dd.


The timezone in my application is set within config/app.php like so.


  /*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/

'timezone' => 'Europe/London',

Fairly recently in Britain we entered into British Summer Time (as we do every year) and this raises my actual question.


A user used a date picker to select 2022-04-04 and this was entered into the database as 2022-04-04.


The issue is with the return value from an accessor.


/**
* Force date format.
*
* @param [type] $value
* @return void
*/
public function getDateAttribute($value)
{
return Carbon::parse($value)->format('d M Y');
}

(Note that the date field has also been cast to a Carbon instance.)


This comes back as 03 Apr 2022, and if you add Y-m-d H:i:s it comes back as 2022-04-03 11:00



Given the picture above can I assume that Europe/London is equivalent to GMT?


Update


I failed to mention that I'm using vue-datepicker so when I select a date it looks like this:



I then pass this through moment.js like so.


"date": moment(this.date),


Which sends through as follows



Update 2


I send the date to my backend via an axios request and I format it with moment.js like so:


axios.post('/api/expenses/personal', {
"date": moment(new Date(this.date)).format('YYYY-MM-DD'),
"business_reason": this.business_reason,
"total_before_vat": this.total_before_vat,
"category": this.category,
"centre_cost_centre_id": this.centre_cost_centre_id,
"is_foreign_expense": this.is_foreign_expense,
"total_before_conversion": this.total_before_conversion,
"conversion_rate": this.conversion_rate,
"currency": this.currency,
"has_vat": this.has_vat,
"vat_claimed": this.vat_claimed,
"total": this.calculatedTotal,
"expense_group_id": this.$route.params.expense_group_id
})


My Answer:
Europe/London is the timezone for the UK, which includes both GMT (Greenwich Mean Time) and BST (British Summer Time). During British Summer Time, the timezone is GMT+1.

In your case, it seems like there might be a discrepancy in how the date is being handled between the frontend and backend. When you select a date in the frontend using vue-datepicker and pass it through moment.js, make sure that the date is being formatted correctly and taking into account the timezone.

When sending the date to the backend via axios, make sure that the date is being formatted correctly and consistently with the timezone settings in your Laravel application. You may need to adjust the formatting or conversion of the date to ensure that it is stored and retrieved correctly in the database.

Overall, it's important to ensure that the date handling is consistent and accurate across both the frontend and backend to avoid any timezone-related issues.

Rate this post

3 of 5 based on 5662 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us