JavaScript error before PHP, adds about a month and a half to the time

Ok, I am trying to convert the javascript label to the php equivalent, I looked through the messages here on the stack for a decent answer, as well as for various other sites, so this question comes as a last effort, despite the fact that I know that I can get and someone tells me nothing more than a question of cheating from another place. So despite this ..

This is my failure attempt so far. Please note: I am using jQuery, and noConflict, therefore j , not $

 var yr = j('#time_year :selected').val();//year var mn = j('#time_month :selected').val();//month var dy = j('#time_day :selected').val();//day var hr = j('#time_hour :selected').val();//hour var min = j('#time_minute :selected').val();//minute var sc = j('#time_second :selected').val();//second if(j('#time_ampm :selected').val() == 12 && j('#time_hour :selected').val() < 12){hr = j('#time_hour :selected').val()+12;} if(j('#time_ampm :selected').val() == 0 && j('#time_hour :selected').val() == 12){hr = 0;} var theDate = new Date(yr, mn, dy, hr, min, sc).getTime(); setTimeStamps(theDate); j('#timestamp_now_js').html(" "+theTime); j('#timestamp_now_php').html(" "+Math.round(theTime / 1000)); 

The selection values ​​at the top are based on a link to a Mozilla.org link using 0-nn as they are described. Where, like am / pm if-else, in essence, means to add 12 hours to the selected time, since the time displayed on the site is a 12-hour format. Javascript uses 24 hours. Its also there, if am is selected and using 12, it converts 12 to 0 to fit the 24-hour formatting concept.

Currently, it seems to be happening that, although it works, when I run the timestamp set using the php date() function, it shows a date that is about a month 2 weeks and a few days, the chapter of that that I'm testing from, which is equivalent now

I would create a jsFiddle of everything that was included in html, but I use php to create my selection fields. Thus, if you want to see that I have a "live", go to http://7pz.net/timestamps.php , as I am testing the concept.

+4
source share
1 answer

This is because in Javascript Date objects, the month range is 0-11, not 1-12. This is for the "monthly" part. Try the following:

 var mn = j('#time_month :selected').val() - 1; //month 

Or better, generate <option> with values ​​from 0 to 11.

Plus, this statement:

 hr = j('#time_hour :selected').val()+12; 

Remember that j('#time_hour :selected').val() is a string, not a number, so you need to convert it before adding 12, otherwise you will get something like "0412" . You create a date with the clock set to 412, i.e. 17 days and 4 hours. And 17 days are added to the daily account.

Fix it like this:

 hr = +j('#time_hour :selected').val() + 12; 

I used the plus unary operator .

(PS: there are many opportunities to improve your code, starting with the readability and caching of jQuery objects. Spend some time improving your Javascript coding skills before getting used to some bad practices.)

+2
source

All Articles