Time format in laravel migration?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Handling Time Format in Laravel Migrations - EU-Style Timestamps Made Simple

When working with databases in your application, it's essential to handle time format correctly. In this comprehensive guide, we'll explore how to use and store time in European (EU) format like 12:00 or 21:34 with Laravel migrations. We'll also cover some best practices for handling timestamp data types to ensure your application stays organized and efficient.

Understanding the EU Time Format

In Europe, time is commonly represented using a 24-hour format known as 'hh:mm'. This format allows us to indicate both daytime and nighttime hours clearly. For instance, 00:00 represents midnight, while 12:00 stands for noon. Similarly, 21:34 denotes an evening hour.

Migrating the Time Data Type in Laravel

To handle timestamps correctly within your Laravel application, we'll need to create a migration file and configure it appropriately. Let's take a look at a sample code snippet: ```php Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('arena'); // Use the 'DateTime' format to store EU-style timestamps: $table->dateTime('beginn')->format('h:i'); $table->timestamps(); }); ``` Here, we set up a new database table called 'posts' using the create method. We define several columns with different data types: primary key 'id', string values for the 'title' and 'arena', and dateTime column named 'beginn' to store EU-style timestamps. Finally, we enable timestamps for this table, allowing Laravel to manage created_at and updated_at fields automatically. Remember that the format is applied during database creation or migration running time. We can use the 'format' method to apply our desired format when inserting data into the table as well: ```php $post = new Post; $post->title = "Sample Post"; $post->arena = "Example Arena"; // Format the timestamp before storing it in the database: $beginn_formatted = \Carbon\Carbon::parse("21:34")->format('h:i'); $post->beginn = $beginn_formatted; $post->save(); ```

Handling Date and Time Separately

Sometimes, you might need to handle date and time separately. In such cases, use the 'date' and 'time' data types, respectively: ```php Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('arena'); // Store date as 'yy-mm-dd' format: $table->date('start_date'); // Store time as 24 hours format (hh:mm): $table->time('beginn'); $table->timestamps(); }); ``` Now that we understand the principles of handling EU-style timestamps in Laravel migrations, let's ensure our databases remain organized and efficient. Remember to use appropriate data types for your specific requirements and always keep backups to avoid data loss. If needed, refer to relevant resources such as https://laravelcompany.com/blog/handling-date-and-time-data-types-in-laravel to gain a deeper understanding of handling date and time in Laravel applications.