2016-03-22 28 views
5

Laravelで現地時間で時刻を印刷したいと思います。ユーザーが投稿を作成すると、作成された時刻がサーバーに表示されます。現地時間にどのように表示することができますか?私が作成した時間を表示するには、このコードを使用し、私のブレードファイルでLaravelでサーバー時間を現地時間に変換する方法は?

、サーバの時刻であるデータベースの時間が表示され

{{{ $posts->updated_at }}} 

。どのように私はそれをユーザーの現地時間に変換できますか?

+0

あなたの基本的なオプションは、1)あるapp.phpページでタイムゾーンの設定を使用すると、タイムゾーンについてのより良い情報を持って、このクライアント側を実行します。あなたの応答にいくつかのJSONのUTCタイムスタンプが含まれているようにして、そのタイムスタンプを現地時間で表示するためにJavascriptを書いてください。 2)ユーザーにどのタイムゾーンがあるかをサーバーに伝えます(これはやや難しいかもしれません)。サーバー側で変換を実行します。 –

答えて

2

を参照してください、私はセッションを使用して、ローカル時刻に時間を変換するために解決策を見つけました。現在のタイムゾーンオフセットは、ユーザーの時間を計算するためにセッションに格納されます。ユーザーにタイムゾーンオフセットをセッションに投稿するjquery post関数を作成します。これは、

Route::post('ajax/set_current_time_zone', array('as' => 'ajaxsetcurrenttimezone','uses' => '[email protected]')); 

にHomeController routes.phpの

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

私のコードです。PHP

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

index.blade.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); 
    } 
} 

ヘルパー/ 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("Y-m-d H:i:s", $attr); 
    } 
    return attr; 
} 

今、私たちはクライアントのタイムゾーンの時刻を印刷することができます。タイムゾーン設定コードは、セッションが空の場合にのみ実行されます。

0

これを試してみてください:

$dt = new DateTime($posts->updated_at); 
$tz = new DateTimeZone('Asia/Kolkata'); // or whatever zone you're after 

$dt->setTimezone($tz); 
echo $dt->format('Y-m-d H:i:s'); 
+0

このコードを使用すると、時刻をGMT +5.30に変換することができます。日付は任意の場所に表示されます –

+0

あなたはそれを変換したいユーザーの場所ごとに意味しますか? –

+0

はい@vipul sorathiya –

1

は、この方法を試してみてください、私はこれは何をしたいと思う:ここ

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

は、$this->auth->user()->timezoneは、現在のユーザーのタイムゾーンを返し、timezone()は、ユーザーのににcreated_atを変換します現地時間。

すべての訪問者のタイムゾーン(ログインしているユーザーだけでなく)を取得したい場合は、laravel-geoipと同様のLaravelパッケージを作成できます。 JavaScriptを使用してこれを行うことができます

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

変更なし、それは同じ時刻を表示します –

+0

どのように正確ですそれを使用しようとしている?フルコードを表示できますか? –

4

:それはあなたがこのように使用することができているあなたのための$visitor['timezone']を生成します。

  1. moment.min.js(http://momentjs.com/downloads/moment.js
  2. 瞬間、タイムゾーン-と-data.js()ここで
  3. jstz.min.js(http://pellepim.bitbucket.org/jstz/

:以下のライブラリを使用しますコードは次のとおりです。

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's timezone 
var localTime = momentJsTimeObj.clone().tz(localTimeZone.name()).format(); //convert server time to local time of user 

今、あなたはJS

介してローカル時刻を表示することができます
-2

コメント

//'timezone' => 'UTC', 
関連する問題