Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:16:15+00:00 2026-06-09T14:16:15+00:00

I have been trying to convert between timezones a given date and time. Code

  • 0

I have been trying to convert between timezones a given date and time.

Code speaks more than words, so here it is:

/**
 * Returns the date and time in the new timezone.
 *
 * @param string $datetime:
 *          the date and time to change between timezones
 * @param string $input_tz:
 *          the input timezone
 * @param string $output_tz:
 *          the output timezone
 * @return string The new date and time
 */
public function changeDateTime($datetime, $input_tz, $output_tz) {
    if($input_tz == $output_tz) return $datetime;
    /*
     * We calculate the hour and minute offset from GMT
     */
    date_default_timezone_set($output_tz);
    $out_dst = date('I', $datetime) ? 1 : 0;
    $out_hour_offset = intval(substr(date('O', $datetime), 1, 3)) + $out_dst;
    $out_minute_offset = intval(substr(date('O', $datetime)), - 2);

    date_default_timezone_set($input_tz);
    $in_dst = date('I', $datetime) ? 1 : 0;
    $in_hour_offset = intval(substr(date('O', $datetime), 1, 3)) + $in_dst;
    $in_minute_offset = intval(substr(date('O', $datetime)), - 2);

    /*
     * We subtract hour and minute offsets to come up with total difference
     */
    $hour_offset = $out_hour_offset - $in_hour_offset;
    $minute_offset = $out_minute_offset - $in_minute_offset;

    /*
     * Now we must take care of changing the day/month/year if necessary, as
     * well as the hour/minute for $datetime, and return that value.
     */
    $date = new DateTime($datetime);
    if($hour_offset > 0) {
        $date->add(date_interval_create_from_date_string($hour_offset . ' hours'));
        if($minute_offset > 0) $date->add(date_interval_create_from_date_string($minute_offset . ' minutes'));
    } else if($hour_offset < 0) {
        $date->sub(date_interval_create_from_date_string($hour_offset . ' hours'));
        if($minute_offset > 0) $date->sub(date_interval_create_from_date_string($minute_offset . ' minutes'));
    }
    return $date->format('Y-m-d H:i:s');
}

However, it does not seem to work well. This is the code I am running to test whether it works or not:

$newdatetime = $gato->changeDateTime("2012-08-10 11:33:33", 'Europe/London', 'Europe/Madrid');
echo $newdatetime;

And this is my expected output: 2012-08-10 12:33:33
But this is my actual output: 2012-08-10 11:33:33, which means there is no change in time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:16:17+00:00Added an answer on June 9, 2026 at 2:16 pm

    OK, try this instead:

    function changeDateTime($datetime, $input_tz, $output_tz) {
    
        // Return original string if in and out are the same
        if($input_tz == $output_tz) {
            return $datetime;
        }
    
        // Save current timezone setting and set to input timezone
        $original_tz = date_default_timezone_get();
        date_default_timezone_set($input_tz);
    
        // Get Unix timestamp based on input time zone
        $time = strtotime($datetime);
    
        // Start working in output timezone
        date_default_timezone_set($output_tz);
    
        // Calculate result
        $result = date('Y-m-d H:i:s', $time);
    
        // Set timezone correct again
        date_default_timezone_set($original_tz);
    
        // Return result
        return $result;
    
    }
    
    $out = changeDateTime("2012-08-10 11:33:33", 'Europe/London', 'Europe/Madrid');
    var_dump($out);
    

    Rather than messing about doing all that complicated maths, just let PHP do all the hard work for you 😉

    See it working

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to convert Date of birth from my DB into DATE
Basically on .show() I've been trying to have all of the inputs convert to
I have been trying to figure out a way to convert bitmap files into
I have this query that i've been trying to figure out how to convert
I have been fighting with this for a bit now. I’m trying to convert
I have been trying to convert my rails 2.2.2 app over to jQuery, and
I have been trying to convert sdot like this html_entity_decode(&sdot;); Any tips?
I have a string containing a local date/time and I need to convert it
I have a .NETCF project that I have been trying to convert from .NETCF
I have been trying for ages to convert a string taken from an input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.