Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Laravel: convierte el tiempo a la zona horaria del usuario en el modelo

Tengo el siguiente modelo que devuelve datos de Dynamo AWS, mis datos tienen un tiempo en EPOCH y los convierto a la fecha formateada como se muestra a continuación:

 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:Ymd H:i:s', ]; }

Sin embargo, la hora de regreso no coincide con la hora en que el usuario está viendo los datos y es por eso que me gustaría hacer la conversión usando la zona horaria de registro como un campo adicional en el usuario:

 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; }

Intenté hacer algo como esto: ACTUALIZAR

 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:Ymd 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("Ymd H:i:s")); return $value->setTimezone($timezone)->toDateTimeString("Ymd H:i:s"); } }

Ni siquiera está llamando a mi función, cuando hago esto:

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

Pero no funciona, ¿alguien me puede ayudar a hacer esta conversión?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Diría leer más sobre Accessors y Mutators

seria algo como

 public function getEPOCHAttribute($value) { //Logic to change the date return $value; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!