On this page, we bring you simple PHP code for a Time zone converter.
<?php
// Set the default time zone
date_default_timezone_set('UTC');
// Define the input time zone and time
$input_timezone = 'America/New_York';
$input_time = '2023-03-01 12:00:00';
// Define the output time zone
$output_timezone = 'Europe/London';
// Create a DateTime object for the input time
$input_datetime = new DateTime($input_time, new DateTimeZone($input_timezone));
// Convert the input time to the output time zone
$output_datetime = clone $input_datetime;
$output_datetime->setTimezone(new DateTimeZone($output_timezone));
// Format the input and output times for display
$input_time_display = $input_datetime->format('Y-m-d H:i:s T');
$output_time_display = $output_datetime->format('Y-m-d H:i:s T');
// Output the results
echo "Input time: $input_time_display<br>";
echo "Output time: $output_time_display";
?>
This code first sets the default time zone to UTC. Then, it defines the input time zone and time, as well as the output time zone.
Next, it creates a DateTime object for the input time in the input time zone. It then clones this object and sets the time zone of the clone to the output time zone. This effectively converts the input time from the input time zone to the output time zone.
Finally, it formats the input and output times for display and outputs the results.
Note that you can modify the input and output time zones and times as needed for your use case.
Leave a comment