John Davidson

php - Laravel - convert time to user's timezone in model

0 comments
Message:


I have the following model that returns data from Dynamo AWS, my data has a time in EPOCH, and I convert it to the formatted date as below:


class File extends BaseDynamo
{
protected $table = 'Dados';
protected $primaryKey = 'SUI';
protected $sortKey = ['EPOCH','desc'];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'EPOCH' => 'datetime:Y-m-d H:i:s',
];
}

However, the time returning does not match the time that the user is seeing the data and that is why I would like to make the conversion using the registration timezone as an extra field in the user:


private function timezone(){
$user = User::find(Auth::id());
$extra_field = ExtraField::where('description','=','TimeZone')->first();

if($extra_field) {
$timezone = ExtraFieldValue::where('extra_field_id', '=', $extra_field->id)->where('user_id', '=', $user->id)->first()->value;
} else {
$timezone = 'America/Sao_Paulo';
}

return $timezone;
}

I tried to do something like this:
UPDATE


class File extends BaseDynamo
{
protected $table = 'Dados';
protected $primaryKey = 'SUI';
protected $sortKey = ['EPOCH','desc'];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'EPOCH' => 'datetime:Y-m-d H:i:s',
];

public function getEpochAttribute($value)
{
$user = User::find(Auth::id());
$extra_field = ExtraField::where('description','=','TimeZone')->first();

if($extra_field) {
$timezone = ExtraFieldValue::where('extra_field_id', '=', $extra_field->id)->where('user_id', '=', $user->id)->first()->value;
} else {
$timezone = 'America/Sao_Paulo';
}
Log::info('timezone');
Log::info($value);
Log::info($timezone);
Log::info($value->setTimezone($timezone)->toDateTimeString("Y-m-d H:i:s"));

return $value->setTimezone($timezone)->toDateTimeString("Y-m-d H:i:s");
}
}

It's not even calling my function, when I do this:


$collection = collect(File::filterBetween('EPOCH', $date)->filter('SUI','=',$asset->mac_id)->scan());
$my_datetime = $collection->whereIn($variable->key, [0,1])->pluck('EPOCH')->last();

But it doesn't work, can someone help me how to make this conversion?



My Answer:
To convert the time to the user's timezone in the model, you can use the `getEpochAttribute` method in your `File` model. Here's an updated version of your code:

php
class File extends BaseDynamo
{
protected $table = 'Dados';
protected $primaryKey = 'SUI';
protected $sortKey = ['EPOCH','desc'];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'EPOCH' => 'datetime:Y-m-d H:i:s',
];

public function getEpochAttribute($value)
{
$user = User::find(Auth::id());
$extra_field = ExtraField::where('description','=','TimeZone')->first();

if($extra_field) {
$timezone = ExtraFieldValue::where('extra_field_id', '=', $extra_field->id)->where('user_id', '=', $user->id)->first()->value;
} else {
$timezone = 'America/Sao_Paulo';
}

$dateTime = new DateTime($value);
$dateTime->setTimezone(new DateTimeZone($timezone));

return $dateTime->format('Y-m-d H:i:s');
}
}


In this code, we are using the `getEpochAttribute` method to convert the EPOCH time to the user's timezone. We first retrieve the user's timezone from the database or use a default timezone if not found. Then, we create a new `DateTime` object with the EPOCH time and set its timezone to the user's timezone. Finally, we format the datetime object and return it.

Make sure to replace the `Auth::id()` with the actual user ID if you are not using Laravel's authentication system. Also, ensure that the `ExtraField` and `ExtraFieldValue` models are correctly defined and linked to the `User` model.

Rate this post

3 of 5 based on 2520 votes

Comments




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