Home > other >  Multiple timezone Laravel how to show correct time with time picker
Multiple timezone Laravel how to show correct time with time picker

Time:10-20

I'm trying to implement multiple timezone in laravel. I have already JS which collect users Timezone and successfully stored in 'timezone' column in Users table.

I have a view blade where user can choose the date and the time to schedule when their campaign will be sent. Im struggling to pass the users timezone to be shown in the view ("h:i A",). At the moment shows the timezone from config/app.php which is in my case 'Europe/Amsterdam'.

Here is the part of the view with bootstrap datepicker and time.


<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 hljs-number">6">
                                           
                                           <div hljs-string">">
                                           <input type="text"  name="send_time"  value="{{ date("h:i A", $ts) }}" id="timepicker" hljs-string">">
                                           <span hljs-string">"><i hljs-string">"></i></span>
                                           <span hljs-string">">CET</span>

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

I tried to add this kind of attribute in my model but Im not sure which arguments I should add here to get time shown correctly in the view:


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

Thanks for your help!

CodePudding user response:

I believe the easiest solution would be to add the following to the boot method in AppServiceProvider.php

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

If that doesn't work, you can try replacing time() with laravel's helper method - now(UTC) and provide the timezone as a parameter.

Also, change strtotime as in the following:

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

CodePudding user response:

you can change datetime format by following

Carbon doc

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