Counterize II を 有効にしてみた が、今日カウンターが 0 リセットされ、昨日カウンターが入れ替わるタイミングがおかしい。0 時に切り替わるべきところが 9 時に切り替わる。9 時間のズレってことは UTC <-> JST の差。タイムゾーン。
もちろん WordPress の 一般設定 でタイムゾーンを UTC +9 に設定している。
調べてみると WordPress 2.9 での仕様変更が影響しているらしい。wp-settings.php によって date_default_timezone_set(‘UTC’) されるようになった。つまり date() 関数と time() 関数は UTC に固定されている。
テンプレートやプラグインでは date() 関数と time() 関数の代わりに date_i18n() 関数を使うと幸せになれるようだ。
Counterize II の date() 関数と time() 関数を date_i18n に置き換える
sudo cp -a counterize.php counterize.php.$(date +%Y%m%d%H%M%S) sudo sed -i "s/ date(/ date_i18n(/" counterize.php sudo sed -i "s/ time()/ date_i18n(\"U\")/" counterize.php
diff -urN counterize.php.20100603021412 counterize.php
--- /var/www/wordpress/wp-content/plugins/counterizeii/counterize.php.20100603021412 2010-05-25 22:38:32.000000000 +0900
+++ /var/www/wordpress/wp-content/plugins/counterizeii/counterize.php 2010-06-21 01:25:42.000000000 +0900
@@ -232,7 +232,7 @@
# Returns amount of hits today.
function counterize_gethitstoday()
{
- $today = date("Y-m-d");
+ $today = date_i18n("Y-m-d");
$sql = "SELECT COUNT(1) FROM ".counterize_logTable()." WHERE timestamp >= '$today'";
$wpdb =& $GLOBALS['wpdb'];
return $wpdb->get_var($sql);
@@ -241,8 +241,8 @@
# Returns amount of hits yesterday.
function counterize_gethitsyesterday()
{
- $today = date("Y-m-d");
- $yesterday = date("Y-m-d", time()-86400);
+ $today = date_i18n("Y-m-d");
+ $yesterday = date_i18n("Y-m-d", date_i18n("U")-86400);
$sql = "SELECT COUNT(1) FROM ".counterize_logTable()." WHERE timestamp >= '$yesterday' AND timestamp < '$today'";
$wpdb =& $GLOBALS['wpdb'];
return $wpdb->get_var($sql);
@@ -251,7 +251,7 @@
# Returns amount of hits during the last 7 days.
function counterize_getlatest7days()
{
- $sevendaysago = date("Y-m-d", time()-86400*7);
+ $sevendaysago = date_i18n("Y-m-d", date_i18n("U")-86400*7);
$sql = "SELECT COUNT(1) FROM ".counterize_logTable()." WHERE timestamp >= '$sevendaysago'";
$wpdb =& $GLOBALS['wpdb'];
return $wpdb->get_var($sql);
@@ -261,7 +261,7 @@
# Returns amount of unique IP's in the last 7 days
function counterize_getuniquelatest7days()
{
- $sevendaysago = date("Y-m-d", time()-86400*7);
+ $sevendaysago = date_i18n("Y-m-d", date_i18n("U")-86400*7);
$sql = "SELECT count(DISTINCT IP) FROM ".counterize_logTable()." WHERE timestamp >= '$sevendaysago'";
$wpdb =& $GLOBALS['wpdb'];
return $wpdb->get_var($sql);
@@ -316,7 +316,7 @@
$sql = "SELECT MIN(timestamp) from ".counterize_logTable();
$wpdb =& $GLOBALS['wpdb'];
$t = $wpdb->get_var($sql);
- return date($dateformat, strtotime($t));
+ return date_i18n($dateformat, strtotime($t));
}
# show the most visited pages
@@ -332,7 +332,7 @@
function counterize_most_visited_pages24hrs($number = 10, $width = 300)
{
- $onedayago = date("Y-m-d", time()-86400);
+ $onedayago = date_i18n("Y-m-d", date_i18n("U")-86400);
$wpdb =& $GLOBALS['wpdb'];
$sql = "SELECT count as amount, p.url as url, p.url as label "
. " FROM " .counterize_logTable(). " m, " . counterize_pageTable(). " p "
@@ -347,7 +347,7 @@
function counterize_most_visited_referrers24hrs($number = 10, $width = 300)
{
- $onedayago = date("Y-m-d", time()-86400);
+ $onedayago = date_i18n("Y-m-d", date_i18n("U")-86400);
$wpdb =& $GLOBALS['wpdb'];
$sql = "SELECT count as amount, r.name as label, r.name as url "
. " FROM ".counterize_logTable(). " m, ". counterize_refererTable(). " r "
@@ -392,7 +392,7 @@
function counterize_most_searched_keywords_today($number = 10, $width = 300)
{
$wpdb =& $GLOBALS['wpdb'];
- $today = date("Y-m-d");
+ $today = date_i18n("Y-m-d");
$number = $wpdb->escape($number);
$sql = "SELECT count(1) as amount, k.keyword as label FROM " . counterize_keywordTable() . " k "
.", " . counterize_logTable() . " l, " . counterize_refererTable() . " r "
@@ -558,7 +558,7 @@
# Returns amount of unique hits today
function counterize_getuniquehitstoday()
{
- $today = date("Y-m-d");
+ $today = date_i18n("Y-m-d");
$sql = "SELECT count(DISTINCT ip) FROM ".counterize_logTable()." WHERE timestamp >= '$today'";
$wpdb =& $GLOBALS['wpdb'];
return $wpdb->get_var($sql);
ただ、この方法では Counterize II しか修正できない。date() 関数や time() 関数を使う古いプラグインが複数ある場合は大変かもしれない。
その場合には WordPress 自体の仕様を元に戻す。wp-settings.php の date_default_timezone_set(‘UTC’) を削除するのも有効かもしれない。ただし、影響範囲は大きくなるし WordPress 開発者たちの意向に反するかもしれない。
wp-settings.php の date_default_timezone_set(‘UTC’) を削除する
cd /var/www/wordpress/ cp wp-settings.php wp-settings.php.old sudo bash -c "grep -v 'date_default_timezone_set' wp-settings.php.old > wp-settings.php" sudo rm -rf wp-settings.php.old
diff -urN wp-settings.php.old wp-settings.php
--- wp-settings.php.old 2010-02-16 23:09:11.000000000 +0900
+++ wp-settings.php 2010-06-03 01:56:19.000000000 +0900
@@ -18,8 +18,6 @@
set_magic_quotes_runtime(0);
@ini_set('magic_quotes_sybase', 0);
-if ( function_exists('date_default_timezone_set') )
- date_default_timezone_set('UTC');
/**
* Turn register globals off.
