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'
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.
Related
I am having trouble in the timestamp.
When I am using the LAMP in Ubuntu, then it works with correct date which I entered, but on other systems it show 1 day back's date.
I don't know what I need to do now. I have stored the timestamp in my database. But when I am showing it on my web application, it works fine in the LAMP but not in others.
When I am converting the timezone to online converter it shows backdated result. What do I do now?
You can set PHP default timezone before reading the date from the timestamp.
Add the following line before reading the date.
date_default_timezone_set('Asia/Calcutta');
Let me know if this helps.
References:
http://php.net/manual/en/function.date-default-timezone-set.php
http://php.net/manual/en/timezones.php
The timestamp stored is correct, Please set the default time zone in your PHP application to let the system know which timezone you are using then it will store the correct time zone. the below link would help you.
I am assuming you are storing timestamp in DB and retrieving it to display
http://php.net/manual/en/function.date-default-timezone-set.php
Thanks
How to get correct date not by system date in php
For ex: actual date is 24/4/2013, but I changed my system date is 25/4/2013.
How can get correct date ie 24/4/2013?
PHP can't inherently know that your system time is incorrect. As others have pointed out, you can query an authoritative source (similar to the solution offered here) instead.
EDIT: shortcut to the time server query example
http://tf.nist.gov/tf-cgi/servers.cgi
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Set the default time zone prior to use the date() function.
For instance:
date_default_timezone_set('UTC');
// or
// date_default_timezone_set('Europe/Madrid')
echo date("Y-m-d H:i:s");
I want to have the date format j/n/Y in my RSS.
e.g: 31/5/2011 display in the RSS file 05/07/2013 00:00:00.
I am using
date ( "j/n/Y" ,time() );
(I'm taking the date from DB)
How are you generating this time string. Is strtotime() involved in any stage in that process? That function, while handy in some situations, is at best "artificially stupid" and will mis-interpret a wide variety of dates as something completely different.
I've got a pretty simple question. I send a date as millis created by javascript to a php site and use phps getdate to get information about the date in an associative array. My timezone is GMT+2, but I do not understand why date conversion using milliseconds should have any effect like this. Can someone please explain this to me?
Thanks!
It sounds like PHP doesn't know what the timezone is supposed to be or the system timezone is not properly set. The simplest fix is to set the timezone you want yourself in the PHP code.
I am trying to figure out why php date() is giving me the wrong time, setting the actual time back 2 hours.
<?php echo date("Y-m-d H:i:s"); ?>
This gives 2011-01-01 03:14:04 instead of 2011-01-01 05:14:04. The hour is decreased by 2.
I have not change my timezone for date() and when users visit the site I want the time to be correct for their timezone also. How can I get this to work using php?
it is because by default it shows GMT time you can change for your region with following code
date_default_timezone_set("Asia/Bangkok");//set you countary name from below timezone list
echo $date = date("Y-m-d H:i:s", time());//now it will show "Asia/Bangkok" or your date time
List of Supported Timezones
http://www.php.net/manual/en/timezones.php
You would have to use either date_default_timezone_set() or a datetime object, and the user would have to set their own timezone in an options menu somewhere.
Otherwise, PHP is a server side language and has no idea what time it is on the user's end.
You would have to use a client side language, JavaScript. You could either have it just be static and display the current user system time, or if for whatever reason you needed to get their time into PHP, you could use some AJAX like scripting to have JavaScript send their time into a script when the page loads.
Try setting the the timezone: date_default_timezone_set or via the ini
Update: you cannot set the correct date for your users. Javascript can handle it but you'd have to rely on the user's system to determine his/her time.
//Change date format
$dateInfo = date_parse_from_format('m-d-Y', $data['post_date']);
$unixTimestamp = mktime(
$dateInfo['hour'], $dateInfo['minute'], $dateInfo['second'],
$dateInfo['month'], $dateInfo['day'], $dateInfo['year']
);
$data['post_date']=date('Y-m-d',$unixTimestamp);