Recent Topics

[Hack] Calculate Time Difference [b2evo 1.8]

Started by on Jun 30, 2006 – Contents updated: Jun 30, 2006

Jun 30, 2006 09:16    

This hack will add the following to your locales page:
Calculate Time Difference: [Without Minute Difference] [With Minute Difference]

Minute difference is just if the minutes between your time and the servers time have a difference greater than 30 then take that into account.

To apply the hack find line 165 in /blogs/inc/VIEW/settings/_set_regional.form.php
And you should see:

Code

$Form->begin_fieldset( T_('Regional settings') );
  $Form->text_input( 'newtime_difference', $Settings->get('time_difference'), 3, T_('Time difference'),
        array( 'note'=>sprintf( '['. T_('in hours'). '] '. T_('If you\'re not on the timezone of your server. Current server time is: %s.'), date_i18n( locale_timefmt(), $servertimenow ) ), 'maxlength'=> 3, 'required'=>true ) );

If you do, then insert this after it:

Code

// Start of balupton's hack
  $d = split(':',date_i18n( 'H:i', $servertimenow ));
  
  ?>
  <fieldset style="padding-top:0px; margin-top:0px;">
  <div class="label"><label>Calculate Time Difference:</label></div>
  <div class="input" style="padding-top:0px; margin-top:0px;">
  <input type="button" onclick="calc_TimeDifference(false);" value="Without Minute Difference" />
  <input type="button" onclick="calc_TimeDifference(true);" value="With Minute Difference" />
  </div>
  </fieldset>
  <script type="text/javascript">
  
  function calc_TimeDifference(min_dif) {
  
  if ( ntd == null ) {
    var ntd = document.getElementById('newtime_difference');
    
    var server_hours   = <?php echo $d[0]; ?>;
    var server_mins   = <?php echo $d[1]; ?>;
  
    var user_date     = new Date();
    var user_hours     = user_date.getHours();
    var user_mins     = user_date.getMinutes();
  }
  
  //alert('Server Hours: '+server_hours+'\r\nUser Time: '+user_hours);
  
  var result = 0;
  if( user_hours < server_hours ) {
  
    // We times by -1, because there is a negative difference
    result = server_hours - user_hours;
    // We do the following because if the time difference is greater than 12 then we are in a different day
    if( result > 12 ) result = -1*(24-result);
    result *= -1; // Just simplifies things.
    
  } else if( user_hours > server_hours ) {
  
    // We times by -1, because there is a negative difference
    result = user_hours - server_hours;
    // We do the following because if the time difference is greater than 12 then we are in a different day
    if( result > 12 ) result = -1*(24-result);
      
  }
  
  //alert('Time Difference: '+result);
    
  if( min_dif && ! ( user_mins < server_mins+30 && user_mins > server_mins-30 ) ) {
      if( user_mins < server_mins )
        result--;
      else
        result++;
      //alert('Time Difference: '+result+'\r\nMinute Difference Applied');
  }
  
  //alert('Server Time: '+server_hours+'\r\nUser Time: '+user_hours+'\r\nTime Difference: '+result);
    
  // Apply the calculated time difference
  ntd.value = result;
  
  }
    
  </script>
  <?php
  // End of balupton's hack

Then your all done.

Probably a smarter and easier idea would of been using the time zones e.g. gmt+8 etc, but this way works.

Jun 30, 2006 20:35

Slightly modified and committed. Thanks a lot.

Jun 30, 2006 20:43

I know.. ;)

Just to make it clear.. It's been committed to HEAD, but not the 1.8 branch, so it might appear only with the version after 1.8.

Jun 30, 2006 20:46

Oh so b2evo's passed v1.8?


Form is loading...

Social CMS software – This forum is powered by b2evolution CMS, a complete engine for your website.