0

I'll admit I'm a bit of a JS novice, and coming from a PHP background, my idea of scope is clearly different to Javascript's.

// There's a date set, so begin processing
var original_date       = new Date($('input#tour_encoded_dates').val());
var date_search_string  = ''; 
var day_limit           = 14;
var timestamp           = '';

// Go forwards day_limit days
for(var i = 0; i < day_limit; i++) {
    timestamp = strtotime('+'+i+' days', original_date);
    calculated_date = new Date(timestamp).format('Y-m-d');
    date_search_string += calculated_date + ' ';
}

console.log(date_search_string);

The output from console.log() is:

2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10 2013-10-10

I would have expected each iteration to increase the date by one day, but they remain the same.

For reference, if I change the final line of the loop to date_search_string += timestamp + ' '; the output is as follows:

1381363200000 1381363286400 1381363372800 1381363459200 1381363545600 1381363632000 1381363718400 1381363804800 1381363891200 1381363977600 1381364064000 1381364150400 1381364236800 1381364323200

So the issue is clearly with the calculated_date variable - right?

Can someone explain the proper way to do this? Thanks.

7
  • what is the code for JS strtotime function? Commented Oct 10, 2013 at 20:27
  • what is day_limit? is that a number? if it's an array you need to use day_limit.length to get the number of stuff in it. is original_date inside of day_limit? Commented Oct 10, 2013 at 20:28
  • @IlyaBursov I've updated the code for clarity. Commented Oct 10, 2013 at 20:33
  • 1
    I just checked your timestamps: jsfiddle.net/JrVQL - 2013-10-10 is proper result... Commented Oct 10, 2013 at 20:47
  • 2
    please, give us strtotime code, problem is 99% in it Commented Oct 10, 2013 at 20:50

1 Answer 1

2

There is 86400 (i.e. 1/1000 day), between each timestamp.

You are computing (in strtotime) as if timestamps were seconds but they are milliseconds.

Sign up to request clarification or add additional context in comments.

1 Comment

That's it, OP is using strtotime from php.js library, which expects the timestamp in seconds, and multiplies it by 1000, calculate, then divide by 1000 before returning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.