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

  • Home
  • SEARCH
  • 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 3215366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:10:10+00:00 2026-05-17T15:10:10+00:00

I have a cakePHP application that is pulling data from two different databases, which

  • 0

I have a cakePHP application that is pulling data from two different databases, which store dates and times in their data from different timezones. One database’s timezone is Europe/Berlin, and the other’s is Australia/Sydney. To make things more complicated, the application is hosted on a server in the US, and times must be presented to the end user in their local timezone.

It’s easy enough to tell which database I have to access, and so I set the appropriate timezone (using date_default_timezone_set()) in my beforeFind so that the query is sent with a date in the correct timezone.

My problem is then converting the dates in the afterFind to the timezone of the user. I’m passing this timezone through as a named parameter, and to access this in the model I’m using Configure::write() and Configure.read(). This works fine.
The problem is that it seems to be applying my timezone conversion multiple times. For example, if I’m querying the Australia/Sydney database from Australia/Perth the time should be two hours behind, but it’s coming out as six hours behind. I tried echoing the times from my function before and after converting them, and each conversion was working correctly, but it was converting the times more than once, and I can’t figure out why.

The methods I am currently using (in my AppModel) to convert from one timezone to another is as follows:

function afterFind($results, $primary){
    // Only bother converting if the local timezone is set.
    if(Configure::read('TIMEZONE'))
        $this->replaceDateRecursive($results);
    return $results;
}

function replaceDateRecursive(&$results){
    $local_timezone = Configure::read('TIMEZONE');

    foreach($results as $key => &$value){
        if(is_array($value)){
            $this->replaceDateRecursive($value);
        }
        else if(strtotime($value) !== false){
            $from_timezone = 'Europe/Berlin';
            if(/* using the Australia/Sydney database */)
                $from_timezone = 'Australia/Sydney';
            $value = $this->convertDate($value, $from_timezone, $local_timezone, 'Y-m-d H:i:s');
        }
    }
}

function convertDate($value, $from_timezone, $to_timezone, $format = 'Y-m-d H:i:s'){
    date_default_timezone_set($from_timezone);
    $value = date('Y-m-d H:i:s e', strtotime($value));
    date_default_timezone_set($to_timezone);
    $value = date($format, strtotime($value));

    return $value;                    
}

So does anyone have any ideas as to why the conversion is happening multiple times? Or does anyone have a better method for converting the dates? I’m obviously doing something wrong, I’m just stuck as to what that is.

  • 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-05-17T15:10:10+00:00Added an answer on May 17, 2026 at 3:10 pm

    I worked out a solution. I didn’t really understand what the $primary parameter in the afterFind was for until now. So to fix my code above, all I have to do is change the if in the afterFind to the following:

    function afterFind($results, $primary){
        // Only bother converting if these are the primary results and the local timezone is set.
        if($primary && Configure::read('TIMEZONE'))
            $this->replaceDateRecursive($results);
        return $results;
    }
    

    As a side note, I am no longer using the date_default_timezone_set() function to do the timezone conversion, either. My convertDate function has changed as follows:

    function convertDate($value, $from_timezone, $to_timezone, $format = 'Y-m-d H:i:s'){
        $dateTime = new DateTime($value, new DateTimeZone($from_timezone));
        $dateTime->setTimezone(new DateTimeZone($to_timezone));
        $value = $dateTime->format($format);
    
        return $value;                      
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CakePHP Application which I want to protect with a password. The
I have an existing web application that I am converting to use CakePHP. The
In my CakePHP application layout I have a section for latest posts that needs
I have an existing PHP/MySQL application that I want to convert over to CakePHP.
I have a CakePHP 1.2 application. I'm running into the case where I need
I've got a model in CakePHP that doesn't have a table, called Upload. I've
Have a n-tire web application and search often times out after 30 secs. How
I have a CakePHP application where I am trying to get a count of
I want to have an application that displays all of my website's external links
I have been busy integrating Wordpress to one of a CakePHP application.Last Monday I

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.