WordPress.org

Make WordPress Core

Changeset 1372


Ignore:
Timestamp:
05/31/04 17:02:09 (11 years ago)
Author:
michelvaldrighi
Message:

added iso8601 handling functions, to avoid ugly nasty conversions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions-formatting.php

    r1361 r1372  
    415415} 
    416416 
     417// computes an offset in seconds from an iso8601 timezone 
     418function iso8601_timezone_to_offset($timezone) { 
     419  // $timezone is either 'Z' or '[+|-]hhmm' 
     420  if ($timezone == 'Z') { 
     421    $offset = 0; 
     422  } else { 
     423    $sign    = (substr($timezone, 0, 1) == '+') ? 1 : -1; 
     424    $hours   = intval(substr($timezone, 1, 2)); 
     425    $minutes = intval(substr($timezone, 3, 4)) / 60; 
     426    $offset  = $sign * 3600 * ($hours + $minutes); 
     427  } 
     428  return $offset; 
     429} 
     430 
     431// converts an iso8601 date to MySQL DateTime format used by post_date[_gmt] 
     432function iso8601_to_datetime($date_string, $timezone = USER) { 
     433  if ($timezone == GMT) { 
     434    preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', $date_string, $date_bits); 
     435    if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset 
     436      $offset = iso8601_timezone_to_offset($date_bits[7]); 
     437    } else { // we don't have a timezone, so we assume user local timezone (not server's!) 
     438      $offset = 3600 * get_settings('gmt_offset'); 
     439    } 
     440    $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]); 
     441    $timestamp -= $offset; 
     442    return gmdate('Y-m-d H:i:s', $timestamp); 
     443  } elseif ($timezone == USER) { 
     444    return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string); 
     445  } 
     446} 
     447 
    417448?> 
Note: See TracChangeset for help on using the changeset viewer.