View Datetime Content using Doctrine

Asked

Viewed 138 times

2

In a Webservice project using Slimframework, I decided to add Doctrine to the BD queries. I decided to follow the guidance of DOCTRINE to work with Datetime and Time Zones according to this site Doctrine Documentation

When trying to recover the value of the bank I used the following code:

  try{
    $repository = $entityManager->getRepository(User::class);
    $usuarios = $repository->findAll();

    foreach ($usuarios as $usuario){
        var_dump( $usuario->getUserDthActivation()) .  "<br/>";
    }
    die;
    return $response
        ->withHeader("Content-Type", "application/json")
        ->write(json_encode($usuarios, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}catch (Exception $e){

}

The printed result was as follows:

Object(Datetimezone)#138 (2) { ["timezone_type"]=> int(3) [""Timezone"]=> string(17) "America/Sao_paulo" } Object(Datetime)#140 (3) { ["date"]=> string(26) "2012-07-01 08:36:35.000000" ["timezone_type"]=> int(3) ["Timezone"]=> string(17) "America/Sao_paulo" }

In my case I just need the information 01-07-2012 08:07:35

What is the correct way to display date content only in dd-mm-YY hh:mm:ss format?

The file that makes this treatment is the following:

class UTCDateTimeType extends DateTimeType
{
/** @var \DateTimeZone */
static private $utc = null;

/**
 * {@inheritDoc}
 */
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
    if ($value === null) {
        return null;
    }

    if (!$value instanceof \DateTime) {
        return null;
    }

    $value->setTimezone((self::$utc) ? self::$utc : (self::$utc = new \DateTimeZone('UTC')));

    return $value->format($platform->getDateTimeFormatString());
}

/**
 * {@inheritDoc}
 */
public function convertToPHPValue($value, AbstractPlatform $platform)
{

    if ($value === null) {
        return null;
    }

    $val = \DateTime::createFromFormat(
        $platform->getDateTimeFormatString(),
        $value,
        (self::$utc) ? self::$utc : (self::$utc = new \DateTimeZone('UTC'))
    );

    if (!$val) {
        throw ConversionException::conversionFailed($value, $this->getName());
    }

    return $val;
  }
}

1 answer

2


If $usuario->getUserDthActivation() return a Datetime valid, you can improve the return by doing so:

$usuario->getUserDthActivation()->format('d-m-Y H:i:s');

Reference:

  • In doing so he brought the following result: Object(Datetimezone)#138 (2) { ["timezone_type"]=> int(3) ["Timezone"]=> string(17) "America/Sao_paulo" } 01-07-2012 08:07:35 How could I take only the date and delete the rest of the information?

  • @Israelzebulon while doing what?

  • When executing the command: $user->getUserDthActivation()->format(’d-m-Y H:i:s'); displayed the above content. But what I want is only part of this information. In case I want to know the correct way to recover only the date/time.

  • @Israelzebulon, absolute sure, because in front the date is formatted, could see your code. So much so that looks the end of the var_dump 01-07-2012 08:07:35 the date was formatted !!! as it is using the code that should be the problem!

  • Yes, the date formatted , now when executing the command, shouldn’t the system bring only the date instead of bringing all this additional information? I will edit the question and post the code that performs this formatting.

  • @Israelzebulon has an edition, it seems to me that comes two objects then access the number 1 and format with the option I put!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.