02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

};<br />

"deserialize" : function(str){...},<br />

"urldecode" : function(str) {...}<br />

Fixing Timezone Inconsistencies<br />

CHAPTER 8 ■ EDITING THE CALENDAR WITH AJAX AND JQUERY<br />

You aren’t passing a time or timezone to the Date object, so that object will default to midnight<br />

Greenwich Mean Time (00:00:00 GMT). This can cause your dates to behave unexpectedly for users in<br />

different timezones. To address this problem, you’ll need to adjust the date <strong>by</strong> the timezone offset using<br />

two built-in Date object methods: .setMinutes() <strong>and</strong> .getTimezoneOffset().<br />

The return value of .getTimezoneOffset() is the difference in the number of minutes between GMT<br />

<strong>and</strong> the user’s timezone. For instance, the return value of .getTimezoneOffset() in Mountain St<strong>and</strong>ard<br />

Time (-0700) is 420.<br />

Using .setMinutes(), you can add the value of the timezone offset to the Date object, which will<br />

return the date to midnight on the given day, no matter what timezone the user is in.<br />

You can make that adjustment using the following bold code:<br />

fx = {<br />

"initModal" : function() {...},<br />

"boxin" : function(data, modal) {...},<br />

"boxout" : function(event) {...},<br />

// Adds a new event to the markup after saving<br />

"addevent" : function(data, formData){<br />

// Converts the query string to an object<br />

var entry = fx.deserialize(formData),<br />

// Makes a date object for current month<br />

cal = new Date(NaN),<br />

// Makes a date object for the new event<br />

event = new Date(NaN),<br />

// Extracts the event day, month, <strong>and</strong> year<br />

date = entry.event_start.split(' ')[0],<br />

// Splits the event data into pieces<br />

edata = date.split('-'),<br />

// Extracts the calendar month from the H2 ID<br />

cdata = $("h2").attr("id").split('-');<br />

// Sets the date for the calendar date object<br />

cal.setFullYear(cdata[1], cdata[2], 1);<br />

// Sets the date for the event date object<br />

event.setFullYear(edata[0], edata[1], edata[2]);<br />

// Since the date object is created using<br />

281

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!