0

I've written some code for a dynamically created graph and it uses Date.UTC() to create formatted dates.

It works completely fine within months, however I'm having trouble with crossing over months, i.e. Feb to March creates 29th, 30th and 31st Feb and now I have found an error that makes the 31st May 1st June.......

I've tried to find an answer but no luck yet. Is there a simple fix that I am overlooking or is there a fix?

s.Set('chart.xmin', Date.UTC(<?php echo $startyear.",".$startmonth.",".$startday.",".$starthour.",".$startminute; ?>));            
    s.Set('chart.xmax', Date.UTC(<?php echo $finishyear.",".$finishmonth.",".$finishday.",".$finishhour.",".$finishminute; ?>));

Cheers

James

Jimbob
  • 13
  • 4
  • Maybe you're expecting the wrong output; `Date.UTC` gives you an _Integer_ in _ms_ since the unix epoch, not a _Date_ instance. Perhaps your function expects `new Date(Date.UTC(y, m, d, h, min, s))` – Paul S. Jun 03 '13 at 11:32
  • Thanks Paul. The Date.UTC is formated later on in the script. It all works within a month like between 5th and 20th May, the problem starts when we cross over months and invalid or wrong dates are created. – Jimbob Jun 03 '13 at 11:46

1 Answers1

0

JavaScript months are 0-11, while PHP's are 1-12. So when you generate JavaScript code in a PHP page, you'll need to subtract 1 from the month.

But wouldn't it be easier to just pass a timestamp? For example:

new Date(<?php echo date_timestamp_get($yourdate) * 1000 ?>)

The * 1000 is there because PHP's timestamp is in seconds and JavaScript's is in milliseconds. But they both use the same reference date - the 1/1/1970 UTC epoch.

Reference PHP date_timestamp_get docs here.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks Matt. I have done that and it all works if the range remains within a month. It is when the range crosses over the month I get the problem. It makes 31st May the 1st June and adds 29th 20th and 31st February which are all invalid dates – Jimbob Jun 04 '13 at 07:41
  • Really? You aren't showing a `-1` anywhere. in the code. Also, why do you need to pass it as separate values? Wouldn't it be much easier to pass a single integer timestamp? – Matt Johnson-Pint Jun 04 '13 at 15:06