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

vishalkpatel's avatar

Best practices to handle user expiry date in laravel

What is the best way to manage user expiry in the web application? I've an application developed in laravel in which I set user expiry date as per the plan duration after the payment is successful. All my dates are stored in UTC timezone. My application timezone is also UTC.

For example, Let's say John has placed an order for 1 month subscription plan on 14th Nov 2019 at 18:30 UTC (+05:30) which is his timezone, the Order created date in database will be 14th Nov 2019 at 13:00 UTC. According to this, application will set expiry date to 14th Dec 2019. I'm only storing date in expiry date field because I want to expire the user service as soon as the beginning of the day i.e 14th Dec 2019 at 00:00:01

I'm displaying all data related to date in user's timezone. John has timezone set to UTC (+5:30) so he will see expiry date to 14th Dec 2019.

Now, let assume that John try to login on 14th Dec 2019 at 02:00 UTC (+05:30) which will be 13th Dec 2019 20:30 UTC. My web application compares everything in UTC so the comparison will be as below:

expiry_date > current_date i.e 14th Dec 2019 UTC > 13th Dec 2019 UTC

In the above case it will allow login because UTC date comparison is evaluates to true. But if from user perspective, if we see, it is wrong because user see the expiry date in his timezone. For example in John's timezone which UTC (+5:30), it is already 14th Dec 2019 which is an expiry date so it should not allow.

I'm not sure, Is this right or wrong. But, I wants to know the best practices to handle expiry date when everything is stored in UTC. I think, I need to compare dates after converting to user's timezone.

Any help on this will be a big help to me.

Thanks

0 likes
3 replies
Snapey's avatar
Snapey
Best Answer
Level 122

You have lost the fidelity of the hours, so you must change the login check to compare the expiry date in the user's timezone.

Alternatively store expiry time and keep the full datetime value so that the account expires mid-way through a UTC day

vishalkpatel's avatar

@snapey Thanks for your feedback. I also think that storing time along with date is a better solution.

tpaksu's avatar

Actually, storing everything in UTC and using UTC for the server time serves well for all timezones. For example, user's expiration time in UTC is 20.12.2020 12:00:00 in UTC and the user has a +02:00 time zone hour difference, the user will see the time as 14:00. And when you expire the user at 12:00 UTC, the user will see that expire at 14:00 in his timezone. You won't change anything except keeping the database date column and server time timezones in sync.

Please or to participate in this conversation.