Question

I'm trying to find out which day based on seconds ( Unixtime ) , but am having great difficulty . I 'm basing that day has 86400 seconds and that every day should be divisible by 86400 seconds

Therefore , I divide a number ( Unixtime ) by 86400 , caught the integer part and multiply by 86400 , I believe the correct result should be the day at 00:00:00 because the Unixtime 0 is equivalent to date 01/01/1970 0:00:00 . Each 86400 seconds should advance one day, however, by a syntax error or logic I'm failing to get the expected result .

For example : 27/11/2013 00:00:00, is equal to ( Unixtime ) 1385517600 . If I divide ( Unixtime ) 1385517600 86400 by 'll 16036.08333333333 as a result . If I multiply the whole part 16036 by 86400 will have as a result ( Unixtime ) 1385510400 . The Unixtime ( Unixtime ) equals 1385510400 date 26/11/2013 22:00:00 .

What I expected to have in this case is the same date 27/11/2013 00:00:00 Unixtime because as the last date is exactly 00:00:00 pm, I expected that the number was a multiple of 86400 .

With hours works perfectly I have the need to do this to be able to group the data into intervals of minutes , hours , day or week. Thus it would be simple I would work only with the range of seconds .

60 minutes time 3600 day 604800 week 604800

The test: In MySQL i execute: SELECT UNIX_TIMESTAMP('2013-11-27 00:00:00'), '2013-11-27 00:00:00', FLOOR((UNIX_TIMESTAMP('2013-11-27 00:00:00') / 86400)) * 86400, FROM_UNIXTIME( FLOOR((UNIX_TIMESTAMP('2013-11-27 00:00:00') / 86400)) * 86400); The result is: "1385517600", "2013-11-27 00:00:00", "1385510400", "2013-11-26 22:00:00"

Was it helpful?

Solution

1385517600 is actually 27/11/2013 02:00:00, and 1385510400 is 27/11/2013 00:00:00. So your algorithm is correct. On Mac OS:

$ date -u -r 1385517600
Wed Nov 27 02:00:00 UTC 2013

$ date -u -r 1385510400
Wed Nov 27 00:00:00 UTC 2013

An extract from MySQL's documentation for FROM_UNIXTIME:

... The value is expressed in the current time zone. ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top