【发布时间】: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();
}
感谢您的帮助!
【问题讨论】: