Jump to content

shared web hosting display different time zone


$php_mysql$

Recommended Posts

to store post time in DB i use

 

`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

 

now the issue is when i post something and fetch time row it shows time according to US standard, what is the solution to not show time in US standard? i tried using htaccess, php.ini but none worked

Link to comment
Share on other sites

i tried to convert the time using

 

$now  = time()+19800;

 

in the function

 

function time_difference($date)
{
		$timezone = "Asia/Calcutta";
		if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);

    if(empty($date)) {
        return "No date provided";
    }

		$periods  = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
		$lengths  = array("60","60","24","7","4.35","12","10");

		$now             = time()+0;
		$unix_date         = strtotime($date);

       // check validity of date
    if(empty($unix_date)) {
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {
		$difference     = $now - $unix_date;
		$tense         = "ago";

    } else {
		$difference     = $unix_date - $now;
		$tense         = "ago";
    }

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
		$difference /= $lengths[$j];
    }

		$difference = round($difference);

    if($difference != 1) {
		$periods[$j].= "s";
    }

    return "$difference $periods[$j] {$tense}";
}

 

still not getting right time. how must i convert the time zone? everytime i make a new post the time says 11 hours ago, i want it to say a few seconds ago

Link to comment
Share on other sites

What version of PHP are your running? date_default_timezone_set requires 5.1 or better.

 

According to the mySql manual:

TIMESTAMP values are converted from the current time zone to UTC for storage, and converted back from UTC to the current time zone for retrieval. (This occurs only for the TIMESTAMP data type, not for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis, as described in Section 9.6, “MySQL Server Time Zone Support”.

 

This means, that when you select a value from that column of the database, it is being returned to you in the server's timezone (the database server's timezone). Note: this timezone conversion has nothing to do with PHP's timezone setting; this is the timezone set in the database server. Converting it to a different timezone could be problematic. If you are running a current version of PHP, you can look at the DateTime class.

 

Personally, I store datetime values in a DATETIME column (not a TIMESTAMP). And I always insert a UTC time into that column. Then with the default timezone already set, I use strtotime and date to get a local datetime to display.

Link to comment
Share on other sites

CREATE TABLE aLog (
ID  INTEGER UNSIGNED AUTO_INCREMENT,
LogTime DATETIME NOT NULL,
LogMsg VARCHAR(255) NOT NULL,
PRIMARY KEY (ID)
)

 

This inserts the current timestamp

INSERT INTO aLog (LogTime, LogMsg) VALUES (UTC_TIMESTAMP(), 'Start Now');

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.