Unix timestamp saving and flot graphs

Hi, I am really stuck with using unix timestamp. I am using emoncms3 but also saving all my data to another mysql table to have all values in one row every 10 seconds.

I am using unix timestamp to store the time of the recording and then just read it to create the flot graphs. 

however I have a "concept" question about how should i use Unix timestamp, specially because my online server is -8 hours from my location.

I would like to know what is the correct way of storing an Unix timestamp in terms of GMT, UTC, winter, summer, server vs client time difference.

For example if I am in Spain and want to save a Unix Timestamp, should I take into consideration that its +2hours when saving the value? or +1hours in winter?

Or should I save it in a certain neutral way, and then convert the value when reading it?

this problem has also been killing me in php so a solution will help me for two issues.

Ive read about date_default_timezone_set but really dont know how to put everything together.



Thank you very much for your time  and I really appreciate any help.

 

Robert Wall's picture

Re: Unix timestamp saving and flot graphs

Unix time is Unix time - it is the same wherever you are in the world. It does not change with daylight savings time. I would save all times as Unix times, then apply the correction for local time (including daylight savings time when appropriate) when you extract and use the data. Bear in mind that when you are analysing and displaying the data, you need to use the time offset for the place and time the data was saved, not for the place and time that you are displaying the data. So if you are looking at this summer's data next winter, you need to offset by 2 hours. And if I (in the UK) am looking at the same data next winter, I apply the same 2 hours offset even though my local time is then the same as Unix time. If you save as local time, you might have a problem with missing and duplicate times when daylight savings time starts and ends.

Sergegsx's picture

Re: Unix timestamp saving and flot graphs

thank you robert for your answer.

What I have seen is that if i call time() in php it give me unix timestamp, however this value is different in case I have set previously a date_default_timezone_set. So I dont know if the time() i get before doing the timezoneset is also affected by the servers location.

I dont know if i am making myself clear, sorry.

​So the thing is, how can I get a true unix timestamp without problems of where the server is located, or any specified date_default_timezone_set in the server?

​Once i save that value, I guess I need to consider when calling the data back from mysql, that i need to go 2 hours back, right? 

 

​thank you so much Robert

Robert Wall's picture

Re: Unix timestamp saving and flot graphs

The way I read the PHP manual http://www.php.net/manual/en/function.time.php, time( ) does not return a value that has been offset by the locale. And that is exactly how it works for me:

The web page:

<html>
<head>
</head>
<body>
<p>
Unix timestamp now is
<?php
$timenow = time();
echo $timenow;
echo ("<br>");
echo strftime("%c", $timenow);
?>
</p>
<p>
Setting date_default_timezone_set('America/Los_Angeles')
<?php
$result = date_default_timezone_set('America/Los_Angeles');
echo ("<br>tzs returns $result");
?>
</p>
<p>
Unix timestamp now is
<?php
$timenow = time();
echo $timenow;
echo ("<br>");
echo strftime("%c", $timenow);
?>
</p>
</body>
</html>

And the result:

Unix timestamp now is 1334520588
Sun Apr 15 21:09:48 2012

Setting date_default_timezone_set('America/Los_Angeles')
tzs returns 1

Unix timestamp now is 1334520588
Sun Apr 15 13:09:48 2012

Are you interpreting the value returned by time( ) - as in strftime( ) above? If so that is where the offset is being applied.

Yes, you do need to apply the correct offset when you extract the time from mysql.

[Edited to add date & time as string]

Sergegsx's picture

Re: Unix timestamp saving and flot graphs

hello Robert, I am really sorry for having taken so long to answer.

Just wanted to thank you a lot for your time on answering my question, with your code and explanation I completely understood unix timestamp at last !!

My database is not storing the correct timestamp and my website is plotting everything correctly thanks also to this...

https://github.com/openenergymonitor/emoncms3/pull/10

 

Once again, thank you Robert !

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.