How to change time zome in different tables in Laravel

时光总嘲笑我的痴心妄想 提交于 2021-01-29 19:34:34

问题


Can you give an idea? I have two tables in to postgresql DB. The are filled with recods by two separate csv files. But the records of the first csv file are with local timestamp. The second csv file is with timestamps UTC. So I need to change the time in local of the view in this table that is filled with records with timezone UTC. I used to deal with that problem before using laravel with this code in every page that I need to do that, for example:

date_default_timezone_set('Europe/Sofia');

The controller:

      function getdata_chart(Request $request)
      {
        $start_date = date('d-m-Y 00:00:00');
        $end_date = date('d-m-Y 23:59:59');
        if($request->start_date != '' && $request->end_date != '')
        {
          // if user fill dates
          $dateScope = array($request->start_date ." 00:00:00", $request->end_date ." 23:59:59");
        } else {
          // default load page - today
          $dateScope = array($start_date, $end_date);
        };
        $students = MeasCanal::whereBetween('recordtime', $dateScope)
      ->selectRaw('recordtime')
      ->selectRaw('max(formattedvalue) filter (where fullname = \'Данни.Кота\') as iazovir')
      ->selectRaw('max(formattedvalue) filter (where fullname = \'Данни.Температура\') as temperatura350')
      ->where(function ($query) {
        $query->where('fullname', 'like', "Язовир.Данни.Кота")
              ->orWhere('fullname', 'like', "ГСК_11_350.Данни.Температура");
      })
      ->groupBy('recordtime')
      ->orderBy('recordtime')
      ->get();

        return response()->json($students);

      }
    return response()->json($students);
}

回答1:


Update your Model MeasCanal and add the following :

Import and use :

use Carbon\Carbon;

Add Function :

/**
     * Get the user's recordtime.
     *
     * @param  string  $value
     * @return string
     */
    public function getRecordtimeAttribute($value)
    {
            return Carbon::parse($value)->timezone('Europe/Sofia')->toDateTimeString();

    }



回答2:


I'm not sure this is what you want but i think you can get the idea.

First Method,

You can use accessor;

public function getCustomTimestampAttribute($value)
{
    // you can use your logic here to set your timezone by table
    $timezone = 'Europe/Sofia';
    return Carbon::parse($this->custom_timestamp)->timezone($timezone)->toDateTimeString();
}

then you can get the value like: $model->custom_timestamp

Second Method,

You can use map;

$customDates = $dates->map(function ($date, $key) {
    $date->custom_timestamp = Carbon::parse($date->custom_timestamp)->timezone('Europe/Sofia')->toDateTimeString();
    return $date;
});

EDIT

In your model(MeasCanal) set recordtime attribute;

public function getRecordtimeAttribute($value)
{
    // you can use your logic here to set your timezone by table
    $timezone = 'Europe/Sofia';
    return Carbon::parse($this->recordtime)->timezone($timezone)->toDateTimeString();
}

then you can simply see the result after the query in your controller like

dd($students);

or even simpler to see:

dd($students->first()->recordtime); // first matched rows recordtime attr.

Note: you cant use accessors with raw queries you should use eloquent models btw.



来源:https://stackoverflow.com/questions/60635641/how-to-change-time-zome-in-different-tables-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!