im@markvi.eu

Hey there! If you want to set a dif­fer­ent time­zone for each user in your Lar­avel App and show their post cre­ation time in their own time­zone, here are the steps you need to follow:

Add a col­umn time­zone #

Add a new col­umn called time­zone” to the users table so that we can store each user’s pre­ferred timezone.

To do this, you need to cre­ate a migra­tion. A migra­tion is like a recipe for mak­ing changes to your data­base. In this case, we’re adding a new col­umn to the users table:

php artisan make:migration add_timezone_to_users_table --table=users

Edit the gen­er­at­ed migra­tion file in database/migrations:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddTimezoneToUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('timezone')->default('UTC');
        });
    }

    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('timezone');
        });
    }
}

Run the migration:

php artisan migrate

Store the user’s pre­ferred time­zone in the time­zone col­umn: #

When cre­at­ing or updat­ing a user, make sure to store their pre­ferred time­zone. You can use a form input or auto-detect the time­zone using JavaScript.

Alter­naliv­ley, to make your time­zone pick­er, you can use the timezone_identifiers_list() func­tion in PHP. It spits out a big ol’ list of all the time­zones that exists.

Cre­ate an acces­sor for the created_​at attribute in the User mod­el to for­mat the date and time accord­ing to the user’s timezone:

Edit your User mod­el locat­ed in app/Models/User.php and add the fol­low­ing accessor:

use Illuminate\Support\Carbon;

class User extends Authenticatable
{
    // ...

    public function getCreatedAtLocalAttribute()
    {
        return Carbon::parse($this->created_at)
            ->timezone($this->timezone)
            ->format('Y-m-d H:i:s');
    }
}

If you want to use an acces­sor in anoth­er mod­el, like Post, here’s an exam­ple you can follow:

use Illuminate\Support\Carbon;

class Post extends Model
{
    // ...

    public function getCreatedAtLocalAttribute()
    {
        return Carbon::parse($this->created_at)
            ->timezone(\Auth::user()->timezone)
            ->format('Y-m-d H:i:s');
    }
}

Dis­play the created_​at in local time: #

In your Blade view, you can now use the created_​at_​local attribute to dis­play the created_at col­umn in the user’s local time:

{{ $user->created_at_local }}

This will dis­play the created_​at attribute in the user’s local time­zone, based on the val­ue stored in their time­zone column.

Con­clu­sion #

Alright, now you can cre­ate the same acces­sor for your time fields in all the mod­els you want. Once you’ve done that, you’ll be able to dis­play the local time for each user in your Lar­avel project. Enjoy! 

Similar Articles