smart watch

Sometimes we need to convert time from one timezone to another. so best thing is to save the time in UTC/GMT and then convert it back to required timezone by php datetime functions.
E.g If we have to send email to users according to their selected time , then first we will save the user time to UTC/GMT and his timezone in the database. and when we are sending emails by running cron jobs or whatever, we will convert the time from user timezone to server timezone and check if the time is same.

I have found this simple function with which we can simply convert time from different timezones.

function converToTz($time="",$toTz='',$fromTz='')
		// timezone by php friendly values
		$date = new DateTime($time, new DateTimeZone($fromTz));
		$date->setTimezone(new DateTimeZone($toTz));
		$time= $date->format('Y-m-d H:i:s');
		return $time;

we will use this function first to convert user time and then checking the time for users timezone. Check for supported timezones by php datetime function Supported Timezones in PHP

$time="2012-01-23 10:30:00"; // User selected time to get email.
$save_time2database=convertToTz($time,'UTC',date('e')); // Convert time from server time to UTC
mysql_query('insert into users set time="'.$save_time2database.'", timezone="'.$timezone.'"'); // and save this time to database

// Check time for sending email for user.
$userTime="2012-01-23 05:00:00";  // Data saved for user in UTC format
$userTimezone="Asia/Kolkata";      // User timezone
if(date('Y-m-d H:i:s')==$userConvertedTime)
mail('','Time to Launch');

Related Posts