note 57088 modified in function.gmdate by danbrown

From: Date: Sun, 20 Jul 2008 03:13:35 +0000
Subject: note 57088 modified in function.gmdate by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142035@lists.php.net to get a copy of this message
Gives user the ability to use their timezone preferences. I had to create this script for a very large community. I first made any posts to the database that would display the date using just time(); Example: mysql_query("INSERT INTO table (datetime) VALUES ('".time()."')"); Also, for their timezone preference, I had the values as (example): (Eastern Timezone) -5 hours (Newfoundland) -3 hours -30 minutes This information would be saved in the users table. To display the date and time in their respective timezone preference: <?php function datetime($datetime,$zone){ if(date('I')):$datetime+=60*60;endif; return gmdate('m-d-Y - h:i:sa',strtotime($zone,$datetime)); } ?> $datetime would be the information pulled from the database from a post for news, forums, etcetera (remember, the inserted table data for the time was using time();) $zone would be the information pulled from the database from the users timezone preference. I also used cookies to store their timezone: $sth=mysql_query("SELECT datetime FROM table LIMIT 1"); $row=mysql_fetch_assoc($sth); echo datetime($row['datetime'],$_COOKIE['timezone']); Remember to set the 'm-d-Y - h:i:sa' to how you wish the time to display. Visit the manual about date(). --was-- Gives user the ability to use their timezone preferences. I had to create this script for a very large community. I first made any posts to the database that would display the date using just time(); Example: mysql_query("INSERT INTO table (datetime) VALUES ('".time()."')"); Also, for their timezone preference, I had the values as (example): (Eastern Timezone) -5 hours (Newfoundland) -3 hours -30 minutes This information would be saved in the users table. To display the date and time in their respective timezone preference: function datetime($datetime,$zone){ if(date('I')):$datetime+=60*60;endif; return gmdate('m-d-Y - h:i:sa',strtotime($zone,$datetime)); } $datetime would be the information pulled from the database from a post for news, forums, etcetera (remember, the inserted table data for the time was using time();) $zone would be the information pulled from the database from the users timezone preference. I also used cookies to store their timezone: $sth=mysql_query("SELECT datetime FROM table LIMIT 1"); $row=mysql_fetch_assoc($sth); echo datetime($row['datetime'],$_COOKIE['timezone']); Remember to set the 'm-d-Y - h:i:sa' to how you wish the time to display. Visit the manual about date(). http://php.net/manual/en/function.gmdate.php

« previous php.notes (#142035) next »