Skip to content

Instantly share code, notes, and snippets.

@davidwindell
Last active December 17, 2015 05:18
Show Gist options
  • Save davidwindell/5556527 to your computer and use it in GitHub Desktop.
Save davidwindell/5556527 to your computer and use it in GitHub Desktop.
<?php
/**
* Handle various type conversions that should be supported natively by Doctrine (like DateTime)
*
* @param mixed $value
* @param string $typeOfField
* @return DateTime
*/
protected function handleTypeConversions($value, $typeOfField)
{
switch($typeOfField) {
case 'datetime':
case 'time':
case 'date':
if ($value === '') {
return null;
}
if (is_int($value)) {
$dateTime = new DateTime();
$dateTime->setTimestamp($value);
$value = $dateTime;
} elseif (is_string($value)) {
$value = new DateTime($value);
}
break;
default:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment