Problems with time and date.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ErickSaint
Forum Newbie
Posts: 3
Joined: Tue Apr 08, 2003 6:52 pm

Problems with time and date.

Post by ErickSaint »

OK from my brief poke around this forum with search, I can tell there are many here who do not like PHP-Nuke, but my question reguards it. I have a few blocks in Nuke that get the time and date from the server machine directly. Here's the problem. My site is hosted on a server in Hong Kong, most of our members are in the central timezone of the US. How can I make the code reflect the 13 hour difference. Here is the code from one such problem. It can be seen "in action" at http://www.pararc.org

Code: Select all

<?php

########################################################################
# PHP-Nuke Block: Total Hits v0.1                                      #
#                                                                      #
# Copyright (c) 2001 by C. Verhoef (cverhoef@gmx.net)                  #
#                                                                      #
########################################################################
# This program is free software. You can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License.       # 
######################################################################## 
# Server Date/Time added by:                                           #
# Nuke Scripts Network (http://www.nukescripts.com)                    #
########################################################################

if (eregi("block-Total_Hits.php", $PHP_SELF)) &#123;
    Header("Location: index.php");
    die();
&#125;

global $nukeurl, $prefix, $startdate, $dbi;
/* Hits for Today */
$t_time = time();
$t_year = date("Y", $t_time);
$t_month = date("n", $t_time);
$t_date = date("j", $t_time);
$result = sql_query("SELECT hits FROM $prefix"._stats_date." WHERE year='$t_year' AND month='$t_month' AND date='$t_date'", $dbi);
list($today) = sql_fetch_row($result, $dbi);
/* Hits for Yesterday */
$y_time = $t_time - 86400;
$y_year = date("Y", $y_time);
$y_month = date("n", $y_time);
$y_date = date("j", $y_time);
$result = sql_query("SELECT hits FROM $prefix"._stats_date." WHERE year='$y_year' AND month='$y_month' AND date='$y_date'", $dbi);
list($yesterday) = sql_fetch_row($result, $dbi);
/* Hits in Total */
$totalhits = 0;
$result = sql_query("SELECT hits FROM $prefix"._stats_year."", $dbi);
while(list($hits) = sql_fetch_row($result, $dbi)) &#123;
    $totalhits = $totalhits + $hits;
&#125;
$content  = "<center><small>"._WERECEIVED."</small><br>\n";
$content .= "<b><a href="modules.php?name=Statistics">$totalhits</a></b><br>\n";
$content .= "<small>"._PAGESVIEWS."<br>$startdate</small></center>";
$content .= "<hr noshade>";
/*$content .= "<center><b><u>..:"._BLATEST." "._BHITS.":..</u></b><br>";*/
$content .= "<center>"._BHITS." "._BTD.": <b><a href="modules.php?name=Statistics&op=DailyStats&year=$t_year&month=$t_month&date=$t_date">$today</a></b><br>";
$content .= ""._BHITS." "._BYD.": <b><a href="modules.php?name=Statistics&op=DailyStats&year=$y_year&month=$y_month&date=$y_date">$yesterday</a></b><br></center>";

/*
$content .= "<hr noshade>";

$sdt = date("F j Y\nH:i:s T");
$zone = date("Z")/3600;
if ($zone >= 0) &#123;
    $zone = "+".$zone;
&#125;
$std = strftime ("%e %B %Y<br>%T %Z");

$content .= "<center>"._SERDT."<br>$sdt (GMT $zone)</center>";
*/
?>
I have tried asking this in the most used PHP-Nuke forums as well, but none have turned a result. Most tell me there is major issues with the International Date Line.

Any help would be greatly appreciated. This has been a thorn in my side for a long time.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Last time I used it I remember there was some sort of time zone adjustment...

or maybe that was phpBB? :? Well phpBB has one for sure, if you snoop around in its source maybe you can see exactaly how they did it. And create a mod for your nuke. Maybe there is a mod out already for this...
ErickSaint
Forum Newbie
Posts: 3
Joined: Tue Apr 08, 2003 6:52 pm

Post by ErickSaint »

Yes indeed there is adjustments for PHPBB which has been taken care of in the settings of it. PHPBB uses GMT some way, it doesnt seem the other blocks are. There is a setting in the mainfile.php of nuke which only controls the time and date for stories and articles posted. That date function is slightly different from the others. That was changed as follows, the -13 value in $datetime line was added to make things work right with that part of Nuke. Unfortunately some blocks for nuke have their own time codes.

Code: Select all

function formatTimestamp($time) &#123;
    global $datetime, $locale;
    setlocale ("LC_TIME", "$locale");
    ereg ("(&#1111;0-9]&#123;4&#125;)-(&#1111;0-9]&#123;1,2&#125;)-(&#1111;0-9]&#123;1,2&#125;) (&#1111;0-9]&#123;1,2&#125;):(&#1111;0-9]&#123;1,2&#125;):(&#1111;0-9]&#123;1,2&#125;)", $time, $datetime);
    $datetime = strftime(""._DATESTRING."", mktime($datetime&#1111;4]-13,$datetime&#1111;5],$datetime&#1111;6],$datetime&#1111;2],$datetime&#1111;3],$datetime&#1111;1]));
    $datetime = ucfirst($datetime);
    return($datetime);
&#125;
ErickSaint
Forum Newbie
Posts: 3
Joined: Tue Apr 08, 2003 6:52 pm

Post by ErickSaint »

So there is no way to force the code to either -13 hours back? If not in hours how about just -1 day, it will not be as accuturate, but for the calendar module the time of day isn't really as important as the actual day. Right now it is still a day ahead, but I have activated the calendar module anyway.

Calendar of Events

Right now, at this hour it looks fine due to the fact that it is 9:10 am CST, but 10:10 pm HKT. This means that by the time many read this the problem will be back to the same in to hours.

Oh well...thanks anyway.
Post Reply