server date time php

Display date and time for local time zone
Hello Reader
I am new in this article world.But i have just start writing articles about my favorite field programming.
my favorite language is php. and i have developed many simple as well as complex projects in php.
So
I am sharing some of my experience with you about time zone.
i know my English is not so good . but i always try to improve it.
So come in the point.
If your server is in UK and your site is visiting from India . you want my site show date & time of ( Eg, products,simple site time,articles submitted date and time ) in local time zone. without editing any thing in the data base.
you can simple get by this function.
Insert query in your data base
CREATE TABLE IF NOT EXISTS `pcp_timezone1` (
`timezid` int(11) NOT NULL auto_increment,
`tz` varchar(250) NOT NULL default '',
`gmt` text NOT NULL,
PRIMARY KEY (`timezid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ;
--
-- Dumping data for table `pcp_timezone1`
--
INSERT INTO `pcp_timezone1` (`timezid`, `tz`, `gmt`) VALUES
(1, 'Pacific/Kwajalein', '(GMT -12:00) Eniwetok, Kwajalein'),
(2, 'Pacific/Samoa', '(GMT -11:00) Midway Island, Samoa'),
(3, 'Pacific/Honolulu', '(GMT -10:00) Hawaii'),
(4, 'America/Anchorage', '(GMT -9:00) Alaska'),
(5, 'America/Los_Angeles', '(GMT -8:00) Pacific Time (US & Canada) Los Angeles, Seattle'),
(6, 'America/Denver', '(GMT -7:00) Mountain Time (US & Canada) Denver'),
(7, 'America/Chicago', '(GMT -6:00) Central Time (US & Canada), Chicago, Mexico City'),
(8, 'America/New_York', '(GMT -5:00) Eastern Time (US & Canada), New York, Bogota, Lima'),
(9, 'Atlantic/Bermuda', '(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz'),
(10, 'Canada/Newfoundland', '(GMT -3:30) Newfoundland'),
(11, 'Brazil/East', '(GMT -3:00) Brazil, Buenos Aires, Georgetown'),
(12, 'Atlantic/Azores', '(GMT -2:00) Mid-Atlantic'),
(13, 'Atlantic/Cape_Verde', '(GMT -1:00 hour) Azores, Cape Verde Islands'),
(14, 'Europe/London', '(GMT) Western Europe Time, London, Lisbon, Casablanca'),
(15, 'Europe/Brussels', '(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris'),
(16, 'Europe/Helsinki', '(GMT +2:00) Kaliningrad, South Africa'),
(17, 'Asia/Baghdad', '(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg'),
(18, 'Asia/Tehran', '(GMT +3:30) Tehran'),
(19, 'Asia/Baku', '(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi'),
(20, 'Asia/Kabul', '(GMT +4:30) Kabul'),
(21, 'Asia/Karachi', '(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent'),
(22, 'Asia/Calcutta', '(GMT +5:30) Bombay, Calcutta, Madras, New Delhi'),
(23, 'Asia/Dhaka', '(GMT +6:00) Almaty, Dhaka, Colombo'),
(24, 'Asia/Bangkok', '(GMT +7:00) Bangkok, Hanoi, Jakarta'),
(25, 'Asia/Hong_Kong', '(GMT +8:00) Beijing, Perth, Singapore, Hong Kong'),
(26, 'Asia/Tokyo', '(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk'),
(27, 'Australia/Adelaide', '(GMT +9:30) Adelaide, Darwin'),
(28, 'Pacific/Guam', '(GMT +10:00) Eastern Australia, Guam, Vladivostok'),
(29, 'Asia/Magadan', '(GMT +11:00) Magadan, Solomon Islands, New Caledonia'),
(30, 'Pacific/Fiji', '(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka');
and
$date='2009-06-29 11:02' // current data and time of server
$timezone='Asia/Calcutta'; // you want indian time zone
$format='F d,Y H:i a' // give format of date and time in which format you want time and date
we give data base query if you want time zone variables ($timezone) comes from data base
convert_tz($date,$timezone,$format);
This is main function this will return local time zone
function convert_tz ($date_str, $tz, $date_format = "r") {
$time = strtotime($date_str);
$tz_bak = getenv("TZ");
putenv("TZ=$tz");
$ret = date($date_format, $time);
putenv("TZ" . ($tz_bak ? "=$tz_bak" : ""));
return $ret;
}
If my artice is helpful for you please give me response on sana.u@rediffmail.com.
if any problem to use please contact me at sana.u@rediffmail.com
About the Author
phpinterviewquestion.com is a interview site , This is for those who want to
Learn about technology, preparation for interview; learn new things in php, .net, javascript, jquery, .htacess, mysql and many more.
phpinterviewquestion.com
[phpbay]server date time php, 30, "11211"[/phpbay]
[phpzon]server date time php, 5, "server"[/phpzon]
PHP Tutorials: Date and Time (Part 2)