$php_mysql$ Posted August 9, 2011 Share Posted August 9, 2011 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 Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted August 10, 2011 Author Share Posted August 10, 2011 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 Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted August 10, 2011 Share Posted August 10, 2011 http://www.gidnetwork.com/b-16.html Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted August 10, 2011 Author Share Posted August 10, 2011 is it possible i could convert the timestamp date and time according to my need? iin this variable before i call for function? $convertTime = date('y-m-d', strtotime($row['time'])); Quote Link to comment Share on other sites More sharing options...
tqla Posted August 10, 2011 Share Posted August 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted August 10, 2011 Author Share Posted August 10, 2011 i tried using date_default_timezone_set('America/Los_Angeles'); but since im on shared hosting it do not let me change anything. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted August 10, 2011 Share Posted August 10, 2011 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. Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted August 10, 2011 Author Share Posted August 10, 2011 could you show me an example of how u store it like in the sql field and how to make the sql row? Quote Link to comment Share on other sites More sharing options...
DavidAM Posted August 10, 2011 Share Posted August 10, 2011 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'); Quote Link to comment Share on other sites More sharing options...
$php_mysql$ Posted August 10, 2011 Author Share Posted August 10, 2011 thanks soo much :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.