【问题标题】:Multiple timezone Laravel how to show correct time with time picker多个时区 Laravel 如何使用时间选择器显示正确的时间
【发布时间】:2021-12-06 18:15:10
【问题描述】:

我正在尝试在 laravel 中实现多个时区。我已经有 JS 收集用户时区并成功存储在用户表的“时区”列中。

我有一个视图刀片,用户可以在其中选择发送广告系列的日期和时间。我正在努力通过用户时区以显示在视图中(“h:i A”,)。目前显示来自 config/app.php 的时区,在我的例子中是“欧洲/阿姆斯特丹”。

这是带有引导日期选择器和时间的视图部分。


<div class="form-group">
                               <div class="col-md-5">
                                   <label for="" class="form-label">Start Sending:</label>
                                   <br>
                                   <input type="radio" name="send_at" @if(old('send_at') != "now") checked @endif value="now" id="send-now">
                                   <label for="send-now">Now</label><br>
                                   <input type="radio" @if($mailing && strtotime($mailing->send_at) > time()) checked @endif name="send_at" value="latter" id="send-latter">
                                   <label for="send-latter">Later</label><br>
                                   
                                   <?php 
                                       $ts = time();
                                       if($mailing && !old('send_date')) {
                                           $ts = strtotime($mailing->send_at);
                                       } elseif(old('send_date')) {
                                           $ts = strtotime(old('send_date') .' '. old('send_time'));
                                       }
                                   ?>
                                           
                                   <div class="row">
                                       <div class="col-md-6 pb-20">
                                           <input class="datepicker form-control" type="text" name="send_date" value="{{ date("m/d/Y", $ts) }}">
                                       </div>
                                       <div class="col-md-6">
                                           
                                           <div class="input-group bootstrap-timepicker timepicker">
                                           <input type="text"  name="send_time"  value="{{ date("h:i A", $ts) }}" id="timepicker" class="form-control input-small">
                                           <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
                                           <span class="input-group-addon">CET</span>

                                       </div>
                                       </div>
                                   </div>
                               </div> 
                           </div>

我试图在我的模型中添加这种属性,但我不确定我应该在此处添加哪些参数才能在视图中正确显示时间:


public function getCreatedAtAttribute($value) 
    {      
       $timezone = optional(auth()->user())->timezone ?? config('app.timezone');
       return Carbon::parse($value)->timezone($timezone);
    }

这是模型中的方法。我还需要在 Send_At 列中转换时间,例如,如果用户使用澳大利亚时间,它将在数据库中写入准确的澳大利亚时间,但由于时间不同并且我的数据库设置为欧洲时间,因此活动将在 8 小时后发送。我不确定如何在该列中转换时间。


protected $table = 'mailings';
   

   public function saveMailing(User $user, $data)
   {
       $subscribers = isset($data['subscribers']) && $data['subscribers'] ? $data['subscribers'] : null;
       $subscribersIds = array();
       
       if($subscribers) {
           $subscribersArr = explode(",", $subscribers);
           if(count($subscribersArr) > 0) {
               foreach ($subscribersArr as $sid) {
                   if($sid > 0) {
                       $subscribersIds[$sid] = $sid;
                   }
               }
           }
       }
       $this->user_id = $user->id;
       $this->promo_id = $data['promo_id'];
       $this->mailinglist_id = $data['mailinglist_id'] > 0 ? $data['mailinglist_id'] : null;
       $this->subject = $data['subject'];
       $this->body = $data['body'];
       $this->send_at = $data['send_at'];
       $this->type = isset($data['type']) && $data['type'] == 'reminder' ? 'reminder' : 'notification';
       $this->subscribers = count($subscribersIds) > 0 ? implode(",", $subscribersIds) : null;
       
       $this->save();
   }

感谢您的帮助!

【问题讨论】:

    标签: php laravel timezone


    【解决方案1】:

    我相信最简单的解决方案是将以下内容添加到 AppServiceProvider.php 中的 boot 方法中

    public function boot()
    {
        date_default_timezone_set('Africa/Lagos');
    }
    

    如果这不起作用,您可以尝试将 time() 替换为 laravel 的辅助方法 - now(UTC) 并提供时区作为参数。

    另外,将strtotime 更改如下:

    Carbon::createFromFormat('Y-m-d H:i:s', $mailing->send_at, 'UTC')
        ->setTimezone('Africa/Lagos')
    

    【讨论】:

    • 嗨法鲁克。我在帖子中添加了我的模型。这工作在 boot() 中。您知道如何在 Send_At 列中转换时间吗?例如,如果用户使用澳大利亚时间,它会在数据库中写入准确的澳大利亚时间,但活动将在 8 小时后发送,因为我的数据库设置为欧洲时间。我不确定如何实现在该列中转换时间。谢谢
    • 你的意思是你的服务器设置为欧洲时间?因为只要您的应用程序中的时区设置符合您的偏好,您的数据库的时区就无关紧要。
    【解决方案2】:

    您可以通过以下方式更改日期时间格式

    Carbon doc

    return Carbon::parse($value)->format('g:i A')->timezone($timezone);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-21
      • 2023-04-09
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-06
      • 2012-08-28
      相关资源
      最近更新 更多