Created
September 1, 2015 11:38
-
-
Save lorenzo/08e24f49c62e0a819868 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Database\Type; | |
use Cake\Database\Type\DateTimeType; | |
/** | |
* Datetime type converter. | |
* | |
* Use to convert datetime instances to strings & back. | |
*/ | |
class TimezoneAwareDateTimeType extends DateTimeType | |
{ | |
/** | |
* Returns the base type name that this class is inheriting. | |
* This is useful when extending base type for adding extra functionality | |
* but still want the rest of the framework to use the same assumptions it would | |
* do about the base type it inherits from. | |
* | |
* @return string | |
*/ | |
public function getBaseType() | |
{ | |
return 'datetime'; | |
} | |
/** | |
* Converts a string into a DateTime object after parseing it using the locale | |
* aware parser with the specified format. | |
* | |
* @param string $value The value to parse and convert to an object. | |
* @return \Cake\I18n\Time|null | |
*/ | |
protected function _parseValue($value) | |
{ | |
date_default_timezone_set('Europe/Copenhagen'); | |
$value = parent::_parseValue($value); | |
date_default_timezone_set('UTC'); | |
$value->timezone = 'UTC'; | |
return $value; | |
} | |
} |
Both examples work to get it from a known source to a known target. In this case I won't know what timezone the user will have set on his computer.
@GuidoHendriks you can read it from the browser and push it via AJAX into the users auth storage (most of the time session) and if it is present there write it via Configure to App.defaultTimezone
. In your type class you can then check if it is set and use it instead self::SOURCE
.
Another way would be to add a 2nd arg array $config = []
to the base classes Type constructor. This would allow us to configure the types. But that's a core change.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is where I ended up.