DateTime conversion function using PHP

0 min read 61 words

It’s really very simple to convert times in different timezones using the following function.

function dateTimeConversion($datetime, 
$timezone="Europe/London") {
  $date = new DateTime($datetime, new DateTimeZone("UTC"));
  $date->setTimezone($timezone);
  return $date->format("Y-m-d H:i:s");
}

As you can see, it takes 2 arguments, $datetime which is a time string and a $timezone which is a timezone to convert to.

A usage example would be:

echo dateTimeConversion("2012-05-01 17:09:58");
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags