Online apps usually have a way to set your timezone so that all times are local to you. One common feature many end-users expect is some mechanism that automatically detects when DST is on/off and changes things accordingly.
The easiest way I've found to do this is with Javascript. It's dreadfully simple code. Here's a sample of what I usually put at the footer of my pages that uses Javascript and PHP:
-
<!-- All of your HTML here -->
-
-
<?php if($do_dst_check): ?>
-
<script type="text/javascript">
-
var timezone = <?php echo $timezone + $dst_offset; ?>;
-
var utc = new Date().getTimezoneOffset() / 60;
-
if (Math.abs(timezone + utc) == 1) {
-
$.ajax({
-
type: 'GET',
-
url: 'ajax_update_dst.php',
-
success: function() { location.reload(); }
-
});
-
}
-
</script>
-
<?php endif; ?>
-
-
</body>
-
</html>
How It Works
First you get the time offset that the user has entered into your application, the one you have on record. You need to get the timezone selection and then the current DST offset. For example, if DST is "on" then that just means you +1 to the users selected timezone.
Javascript has something your server-side scripts might not: it knows about the users locale. So using this fact, we can get the users current timezone offset. This is the "real" offset, the one we know is correct, regardless of what they inputted into your application.
Finally, you simply add the offset your application has on record with the users "real" offset. If the result is exactly -1 or 1 (here I used Math.abs, so -1 becomes 1), that means the information you have on record is incorrect, and the DST status needs to be toggled.
In this example I use a jQuery AJAX call to request a page that would toggle the users DST setting.
Leave a Reply