Laravel date, time and timestamp migration with timezone

In this blog we will discuss laravel date, time, dateTime, and timestamp related migrations with example. laravel migration for mysql dataType date and time related columns. how to add timezone with date time columns.


Laravel date and time related migrations

When you want to make proper date and time dataType columns and also add timezone in mysql then laravel provides many migration to create these columns.

1. Laravel date data type column migration

Laravel migration date() method create mysql DATE dataType column.

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->date('birth_date');
    });
}

2. Laravel time data type column migration

Laravel migration time() method create mysql TIME equivalent column. time() method second parameter is optional and provide information how many digits in miliseconds like example $precision = 5 given this format 00:00:00.00000

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->time('sunrise'); //00:00:00
        $table->time('start_time', $precision = 5); //00:00:00.00000
    });
}

3. Laravel dateTime() migration

Laravel migration dateTime() method create DATETIME dataType column. its have a second optional precision parameter.

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->dateTime('created_at', $precision = 0);
    });
}

4. Laravel timestamp() migration

Laravel migration timestamp() method create TIMESTAMP dataType column with an optional precision.

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->timestamp('created_at', $precision = 0);
    });
}

5. Laravel timestamps() migration

Laravel migration timestamps() method create created_at and updated_at columns

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->timestamps($precision = 0);
    });
}

6. Laravel timestamp() vs dateTimeTz() migration

Laravel migration dateTimeTz() method create DATETIME column with timezone and have an optional precision.

$table->dateTimeTz('created_at', $precision = 0);

7. Laravel time() vs timeTz() migration

Laravel migration timeTz() method same as time() method but it also provide timezone information it makes TIME with timezone column.

$table->timeTz('sunrise', $precision = 0);

8. Laravel timestamp() vs timestampTz() migration

Laravel migration timestampTz() method same as timestamp() method but it also provide timezone information it makes TIMESTAMP with timezone equivalent column.

$table->timestampTz('created_at', $precision = 0);

9. Laravel timestamps() vs timestampsTz() migration

Laravel migration timestampsTz() method same as timestamps() method but it also provide timezone information it makes created_at and updated_at with timezone.

$table->timestampsTz('created_at', $precision = 0);
php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Random tutorial links