[wp-hackers] Internal representation of time

Kimmo Suominen kimmo at global-wire.fi
Fri Mar 14 15:35:26 GMT 2008


On Fri, Mar 14, 2008 at 02:19:21PM +0000, James Davis wrote:
> (see http://trac.wordpress.org/ticket/2149)
> 
> WordPress appears to be a little confusing with it's internal 
> representation of time. Times are stored in MySQL as the DATETIME type 
> but manipulated internally by converting DATETIME to Unix time stamps, 
> which can be easily manipulated with PHP.

A related problem is the fact, that the Unix time stamps are not really
correct when it comes to the gmt and local timestamps.  The database
DATETIME value is always converted to the Unix time stamp that has the
same textual representation in the local timezone of the server.

That is, you get the same value back from mysql2date('U', $datetime),
regardless of whether $datetime comes from post_date or post_date_gmt.

At the minimum I'd like to see 2 new functions in WordPress in this
regard:

function post_gmt_offset($post)
{
    $local = mysql2date('U', $post->post_date);
    $gmt = mysql2date('U', $post->post_date_gmt);

    return $local - $gmt;
}

function post2gmt($post)
    $m = $post->post_date_gmt;
    $i = gmmktime(
	substr($m, 11, 2), substr($m, 14, 2), substr($m, 17, 2),
	substr($m,  5, 2), substr($m,  8, 2), substr($m,  0, 4)
    );
    return $i;
}

> I guess that leaves us with three options
> 
> - Stick to Unix timestamps. Stricter validation to handle input outside 
> the valid range. This requires least change to the code.
> - Switch completely to MySQL timestamps. Stricter validation ...
> - Use an outside library to handle time.
> 
> I'm equally happy with any approach, and coding a patch for it, but 
> what's the feeling on the list?

I think Unix time stamps might be a good way to go since they can be
handled by PHP without any extra code and without having to call on an
external service such as MySQL for calculations.

Best regards,
+ Kimmo
-- 
<A HREF="http://kimmo.suominen.com/">Kimmo Suominen</A>



More information about the wp-hackers mailing list