How to translate server time to local time in Laravel?

I want to print time in local time in Laravel. If the user creates a message, he displays the created time on the server. How can I display it in local time?

In my click file, I used this code to display the generated time,

{{{ $posts->updated_at }}} 

Displays the time in the database, which is the server time. How can I convert it to user local time?

+5
source share
7 answers

I found a solution to convert time to local time using a session. The current time zone offset will be stored in the session to calculate user time. Create a jquery post function to offset the user's time zone for the session. This is my code,

default.blade.php

 @if($current_time_zone=Session::get('current_time_zone'))@endif <input type="hidden" id="hd_current_time_zone" value="{{{$current_time_zone}}}"> // For assigning session value to hidden field. <script type="text/javascript"> $(document).ready(function(){ if($('#hd_current_time_zone').val() ==""){ // Check for hidden field is empty. if is it empty only execute the post function var current_date = new Date(); curent_zone = -current_date.getTimezoneOffset() * 60; var token = "{{csrf_token()}}"; $.ajax({ method: "POST", url: "{{URL::to('ajax/set_current_time_zone/')}}", data: { '_token':token, curent_zone: curent_zone } }).done(function( data ){ }); } }); 

routes.php

 Route::post('ajax/set_current_time_zone', array('as' => 'ajaxsetcurrenttimezone','uses' => ' HomeController@setCurrentTimeZone ')); 

HomeController.php

 public function setCurrentTimeZone(){ //To set the current timezone offset in session $input = Input::all(); if(!empty($input)){ $current_time_zone = Input::get('curent_zone'); Session::put('current_time_zone', $current_time_zone); } } 

helpers /helper.php

 function niceShort($attr) { if(Session::has('current_time_zone')){ $current_time_zone = Session::get('current_time_zone'); $utc = strtotime($attr)-date('Z'); // Convert the time zone to GMT 0. If the server time is what ever no problem. $attr = $utc+$current_time_zone; // Convert the time to local time $attr = date("Ymd H:i:s", $attr); } return attr; } 

index.blade.php

 {{ niceShort($posts->updated_at) }} 

Now we can print the time in the customers time zone. The time zone configuration code is only triggered when the session is empty.

+2
source

You can do this using javascript. Use the following libraries:

Here is the code:

 var update_at = '<?php echo $posts->updated_at;?>'; //set js variable for updated_at var serverTimezone = 'YOUR SERVER TIME ZONE'; //set js variable for server timezone var momentJsTimeObj = moment.tz(update_at, serverTimezone); //create moment js time object for server time var localTimeZone = jstz.determine(); //this will fetch user timezone var localTime = momentJsTimeObj.clone().tz(localTimeZone.name()).format(); //convert server time to local time of user 

Now you can display local time via js

+4
source

The best option is to do this using Javascript, get the time zone of the client, and then convert the server time to clear it.

Please contact fooobar.com/questions/735040 / ...

+2
source

Try this method, I think this is what you want:

 $localTime = $object->created_at->timezone($this->auth->user()->timezone); 

Here $this->auth->user()->timezone will return the user's current time zone, and timezone() convert created_at to the user's local time.

If you want all time zone visitors (not just registered users), you can make the package for Laravel look like laravel-geoip . It will generate $visitor['timezone'] for you, which you can use as follows:

 $localTime = $object->created_at->timezone($visitor['timezone']); 
+1
source

Try the following:

 $dt = new DateTime($posts->updated_at); $tz = new DateTimeZone('Asia/Kolkata'); // or whatever zone you're after $dt->setTimezone($tz); echo $dt->format('Ymd H:i:s'); 
0
source

Go to the app.php page in the Config folder and change this value

 'timezone' => 'UTC', 

to

 'timezone' => 'addYourTimeZoneHere', 

link https://laravel.com/docs/5.5/configuration

find your timezone http://php.net/manual/en/timezones.php

0
source

Comment on time zone settings on app.php

 //'timezone' => 'UTC', 
-2
source

Source: https://habr.com/ru/post/1245528/


All Articles