In my eCommerce store, I often launch sales campaigns that are currently based on my server's time zone. I specify campaigns with details like product IDs, discounts, start and end times, as well as applicable countries. However, this leads to problems when a campaign ends at midnight CEST; customers in other time zones aren't getting a fair shot at the deal, especially during significant sales events like Black Friday. What I want is to set up campaigns that respect different time zones so that when I store the expiration time in my database, it aligns with the time zone of the campaign rather than my server's. Is there a way to achieve this by calculating the right Unix timestamp based on a specified time zone in the campaign?
2 Answers
A good practice is to always store date and time in UTC in your database. This way, you won't need to worry about different time zones messing things up. You can convert the time to UTC when you save it and then convert it back to the user's local time for display. It simplifies everything since you just deal with ISO8601 formatted times. This approach allows for easy handling of date time arithmetic without additional complications.
Using Unix timestamps from the get-go is definitely a good idea! But if your campaigns are clearing based on your server's time zone, you'll need to adjust the timestamp to match the specified time zone. You can calculate the difference between your server's time zone and the campaign's time zone and adjust your timestamps accordingly before storing them. This way, you ensure that everyone gets the same shopping experience, no matter where they are!
Thanks for the suggestion! That's what I thought might work, but just to clarify: should I convert both the `start` and `end` times to Unix format based on the campaign's time zone? That way, when I set the expiration in Valkey, it reflects the correct time for each user?