Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

roulendz's avatar

[L5] Dealing with time, timezones and dates in booking, time managing app?

Hello awesome forum!

I am wondering, about time and date, saving in database.

I am working on event booking app Admin, makes events, users, can book place for event. And admin, sees in calendar how many people have booked table and at what time.

Example would be restaurant there are 10 tables, and people can book any table for any amount of time.

Working hours from 10:00 - 19:00. And at the and 2 persons can not book the same table at the same time.

And admin sees, in calendar from what time witch table is booked.

So the question is in what format I should store date time, Unix, Regular format, and how to deal with timezone.

Also maybe, I could use some package or .js to make it less painful :)

0 likes
6 replies
Intermax's avatar

I would recommend you to use a normal Y-m-d H:i:s format in your databases because it's easier to read than a unix timestamp. Furthermore you can use Carbon for handling dates and timezones in PHP and MomentJS for javascript handling. Sounds like a cool project by the way!

bobbybouwmann's avatar

I would do something like this

migration

$table->dateTime('order_date'); // Or whatever you want to call the field

model

class Serving extends Model {
    
    protected $dates = ['order-date']; 
    // When you fetch the serving model Laravel will create a Carbon instance out of the date from the database 

    // Other model stuff
}

So when you do something like this

dd($serving->order_date); // This will return a Carbon instance.
1 like
constb's avatar

On account of timezones - when you render a Carbon instance to string it doesn't convert datetime to some default timezone, this need to be handled manually. If you and your customers all live in the same timezone, just set it in config/app.php and in database settings.

When managing multiple timezones and different users have different timezone selected, it makes sense to have everything in database and in code UTC-based and perform timezone conversions only before output. Also if users set dates and times in form fields - those are gonna need to be converted as well.

1 like
weerd's avatar

I had a similar question with event dates while also taking into account the time zone. Read a bunch of different things on stackoverflow regarding whether to use DATETIME or TIMESTAMP types when storing in MySQL and still not fully clear. I understand the difference between the two, and while TIMESTAMP incorporates UTC and thus can handle time zone when converted, it seemed to be recommended to save calendar dates as DATETIME.

With that in mind I did see that Laravel has an available migration column type of datetime with time zone using $table->dateTimeTz('created_at');. @bobbybouwmann any experience with this type and handling time zones? It might also be a mute point if Carbon can easily handle a date and converting it to a specified time zone, which I believe is possible :)

bobbybouwmann's avatar

@weerd Yeah Carbon is the perfect match here.

I would recommend you to either store the dates in the same format. So you would store the datetime in the UTC format and then have an extra column with the original timestamp. This way you can always convert it to the correct date. It's also possible to have this timestamp format on your user object for example.

3 likes

Please or to participate in this conversation.