how to Convert Date or Time to any Timezone and Date Format using PHP

In this post we will show you Convert Date or Time to any Timezone and Date Format using PHP, hear for Convert Date or Time to any Timezone and Date Format using PHP we will give you demo and example for implement.

Date manipulation could be a common and intensely helpful development task. it had been ne’er simple using PHP, till PHP5 and DateTime category.

There area unit several cases you would like to convert dates. as an example, if you develop a world net application, each user is in numerous timezone. in addition, it’s helpful to store all dates in utc (GMT), because it is that the Greenwich Mean Time. it’s obvious that you just ought to convert dates from one timezone to a different and from date format prefered by every user to Date Format you employ to store dates (YmdHis could be a sensible choice).

A solution could be the following simple function convert_date():

// call convert_date
function convert_date($dt, $tz1, $df1, $tz2, $df2) 
{
  // create DateTime object hear 
  $date_obj = DateTime::createFromFormat($df1, $dt, new DateTimeZone($tz1));
  // convert timezone hear
  $date_obj->setTimeZone(new DateTimeZone($tz2));
  // convert Date Format hear
  return $date_obj->format($df2);
}

Examples Convert datetime 17/11/2017 02:00:00 in Europe/Athens to UTC timastamp:

convert_date('17/11/2017 02:00:00', 'Europe/Athens', 'd/m/Y H:i:s', 'UTC', 'YmdHis');

It returns: 20171178000000

Convert UTC timastamp 20171178000000 to Europe/Athens datetime with format d/m/Y H:i:s:

convert_date('20171178000000', 'UTC', 'YmdHis', 'Europe/Athens', 'd/m/Y H:i:s');

It returns: 17/11/2017 02:00:00

The complete version for Convert Date or Time to any Timezone and Date Format with PHP

Here we will give you a more advanced syntax of this function, including error handling for execution:

<?php
// Convert a date(time) string to another format or timezone

function convert_date($dt, $tz1, $df1, $tz2, $df2) 
{
	$result = '';
	// check timezone identifiers list
	if(!in_array($tz1, timezone_identifiers_list())) 
	{ 
		// check source timezone hear
		trigger_error(__FUNCTION__ . ': Invalid source timezone ' . $tz1, E_USER_ERROR);
	} elseif(!in_array($tz2, timezone_identifiers_list())) 
	{ 
		// check destination timezone hear
		trigger_error(__FUNCTION__ . ': Invalid destination timezone ' . $tz2, E_USER_ERROR);
	} 
	else 
	{
		// create DateTime object hear
		$date_obj = DateTime::createFromFormat($df1, $dt, new DateTimeZone($tz1));
		// check source datetime hear
		if($date_obj && DateTime::getLastErrors()["warning_count"] == 0 && DateTime::getLastErrors()["error_count"] == 0) 
		{
			// convert timezone hear
			$date_obj->setTimeZone(new DateTimeZone($tz2));
			// convert Date Format hear 
			$result = $date_obj->format($df2);
		} 
		else 
		{
			// show error 
			trigger_error(__FUNCTION__ . ': Invalid source datetime ' . $dt . ', ' . $df1, E_USER_ERROR);//
		}
	}
	return $result;
}
?>

Hope this code and post will helped you for implement Convert Date or Time to any Timezone and Date Format using PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org