Question

Timezones and timestamps confuses me so I'm hoping someone can answer my questions :)

Lets say I have a Python script that parses an RSS feed, converts the date value into a timestamp using the following code and stores it in a database:

article_date = parse(article.published).strftime('%s') if hasattr(article, 'published') else round(time.time())

Now when I retrieve that record from the db in PHP, and I run the following code, does PHP assume the timestamp was UTC-0 and automatically offsets the timezone to Eastern time?

date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s',$timestamp);

I'm seeing weird issues with my dates, so I'm wondering if someone can help me out with advice on how to properly convert and store rss feed timestamps. I can across this line of code somewhere so should I put this at the top of my script?

os.environ['TZ'] = 'Europe/London'
Was it helpful?

Solution

If you want to set your timezones and keep them aligned in PHP and in Python, then your PHP code is completely correct and for python you need to apply the following:

os.environ['TZ'] = 'America/New_York'
time.tzset()

before you call strftime()

That should make sure you store the time in the same zone you're trying to retrieve it.

Note: tzset() is a Unix-only function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top