I have been fiddeling with javascripts timestamps the whole morning now. I keep getting invalid timestamps by running
today = (new Date()).setHours(0,0,0,0) or today = (new Date()).getTime()
These 2 output 1338930000000 and 1338978151748.
I figured this was just a chrome js engine error but reproduced it with Firefox. The expected output of those 2 statements were 1338930000 and 1338977700.
I do, however, have a fix for this. The fix is this:
today = parseInt(((new Date()).setHours(0,0,0,0) * Math.pow(10, -3)).toFixed(0))
or without setHours()
today = parseInt(((new Date()) * Math.pow(10, -3)).toFixed(0))
Is this a common bug? Am I doing something wrong to get these results? The timestamps are in format timestamp * 10^3.
EDIT:
TURNS OUT I WAS EXPECTING THE TIME IN SECONDS (darn you PHP) WHILE JAVASCRIPT OUTPUTS IT IN MILLISECONDS. I am using the phpJS date / time library, and javasctipt timestamp doesn’t seem to be natively compitable with strtotime() and date()
I was trying to work with unix timestamps, those are in seconds.
getTime()returns the number of milliseconds since 1970/01/01, not seconds.