How to Set Unix Timestamp Based on Time Zone for Sales Campaigns?

0
8
Asked By TechieTravler87 On

I'm running an eCommerce store and facing a challenge with my sales campaigns. Currently, all campaigns rely on my server's time zone, which can lead to confusion for customers in different regions. For example, I want to create a campaign that ends at midnight in a specific time zone, say CEST (Berlin time). However, this means the campaign may close during the day for customers in places like the USA or Australia, which is less than ideal.

I currently specify sales in this format:
```json
[
{
"productIds": [ 100, 200 ],
"discountPercentage": 20,
"description": "Test Campaign",
"start": "2025-08-01T00:00:00",
"end": "2025-08-14T23:59:59",
"countries": ["at", "be", "bg", "hr", "cy", "cz", "dk", "ee", "fi", "fr", "de", "gr", "hu", "ie", "it", "lv", "lt", "lu", "mt", "nl", "pl", "pt", "ro", "sk", "si", "es", "se"]
}
]
```

To tackle this, I want to incorporate a `timeZone` property into my campaign setup, allowing me to adjust the expiration of the campaign in Valkey to align with the specified time zone rather than my server's default.

How can I convert the campaign's ending time to a Unix timestamp based on the given time zone? Any suggestions on how to do this?

3 Answers

Answered By DevGuru999 On

It's best practice to store all your date and time in UTC in your database. Make sure your server's time is set to UTC too. This way, when user data comes in, you can convert it to local time when displaying it. You shouldn't have to deal with timezone conversions during the API responses—just return everything in ISO8601 format in UTC. For instance, if you're using Angular for the frontend, it'll handle local conversions automatically for you. On the backend, PHP's DateTime class simplifies date arithmetic and timezone management drastically.

Answered By CoderQueen42 On

You might want to consider using Unix timestamps from the start. This will help prevent the issues you're encountering based on your server's timezone. Essentially, you need to capture the `timeZone` value along with `start` and `end`. When you save the expiration in Valkey, you should calculate the time difference between your server's timezone and the campaign's specified timezone, then adjust your timestamps accordingly.

If you're looking for a precise approach, you need to adjust for daylight saving time differences too. Let me know if you need more details!

Answered By TechieTravler87 On

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.