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

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

You set time zones by using:

date_default_timezone_set('America/Los_Angeles');

 

Here's more info: http://www.php.net/manual/en/timezones.php

 

If you will have users from all over the world using your script then you should get the date from the users computer with javascript.

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.

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');

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.