Expert Group of programming
Loading...

Handling timezone conversion with PHP DateTime

Posted by : Heycode Tech | Posted in : php, WordPress Functions | Jan 17,2016 | 0 comments

As we know that when we call any PHP date function then it will show the SERVER time(Default) but as per client requirement, we need to change the LOCAL time-stamp.

Now sometime, Developer becomes disappointed to change the date in LOCAL timestamp. Now here this article is displaying the some key terms.


Here is the date time function in PHP
string date ( string $format [, int $timestamp = time() ] );
Variable :
i ]- $format = The format of the outputted date string. See the formatting options below.
ii]- timestamp (Optional)
The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().

Let’s see an example of date time conversion :
Here I ‘m explaining in example for GMT date conversion to IST (Indian standard time) date time conversion in simple way.

date-time-conversion

1:Convert GMT time to IST time .

/*
 * Convert GMT time to IST time .
 */
function gmt_to_ist_date($datetime){
	
	$event_length = 330; // IST is 5:30 ahead of GMT, so add 330 minutes
	$timestamp = strtotime($datetime);
	$etime = strtotime("+$event_length minutes", $timestamp);
	return $ist_time = date('d M, Y H:i a', $etime);
	
}
//How to use
gmt_to_ist_date('2016-01-07 12:16:59');

2:Convert GMT time to CST/PST time

/*
 * Convert GMT time to CST/PST time .
 */
function gmt_to_cst_date($datetime){
	
	$event_length = 480; // CST/PST is 8:00 ahead of GMT, so add 480 minutes
	$timestamp = strtotime($datetime);
	$etime = strtotime("+$event_length minutes", $timestamp);
	return $ist_time = date('d M, Y H:i a', $etime);
	
}
//How to use
gmt_to_cst_date('2016-01-07 12:16:59');

3:We can use this function for any time difference.

Here is the format data –

d– Day of the month, 2 digits with leading zeros01 through 31
D – A textual representation of a day, three lettersMon,Sat,Fri
m – Numeric representation of a month with leading zeros01 through 12
M – short textual representation of a monthJan,Feb,Mar
g – 12-hour format without leading zeros 1 through 12
G – 24-hour format without leading zeros 0 through 23
h – 12-hour format with leading zeros 01 through 12
H – 24-hour format with leading zeros 00 through 23
a – Ante meridiem and Post meridiem am or pm
A – Ante meridiem and Post meridiem AM or PM
i – Minutes with leading zeros11,09
s – Seconds, with leading zeros11,09

Some Timezone of country Here is the function of the timezone as well as how to get it.

define('TIMEZONE', 'Asia/Calcutta'); // INDIA
date_default_timezone_set(TIMEZONE);
define('TIMEZONE', 'Asia/Baku'); // Baku, Azerbaijan, Asia
date_default_timezone_set(TIMEZONE);
define('TIMEZONE', 'Asia/Shanghai'); // Shanghai, China, Asia
date_default_timezone_set(TIMEZONE);
define('TIMEZONE', 'Asia/Colombo'); // Colombo, Sri Lanka, Asia
date_default_timezone_set(TIMEZONE);
define('TIMEZONE', 'Australia/Canberra'); // Canberra, Australia
date_default_timezone_set(TIMEZONE);
define('TIMEZONE', 'Europe/London'); // London, England, Europe
date_default_timezone_set(TIMEZONE);
define('TIMEZONE', 'Europe/Berlin'); // Berlin, Germany, Europe
date_default_timezone_set(TIMEZONE);
define('TIMEZONE', 'Europe/Madrid'); // Madrid, Spain, Europe
date_default_timezone_set(TIMEZONE);

Get the default (script) timezone:

date_default_timezone_get();

Get timezone name:

date('T');

Conclusion :
Hope so it will help for date time conversion on different TIME ZONE but still if you have any query then please share with me .

Tags:

Author information - Heycode Tech

Every world can ask question and my website registered user give the suggestion.

See all posts by Author

Connect to author

About us

Heycodetech is a platform where you can ask any questions related to the programming like PHP,jQuery, MYSQL,HTML , CSS and moreHere is the Expert group of programming.