在 Symfony 2 中从数据库中获取时间 [英] Get The Time From Database in Symfony 2

查看:20
本文介绍了在 Symfony 2 中从数据库中获取时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库(时间)中有一个字段的值是 09:00:00.

I have a field in database (Time) with this value 09:00:00.

我创建了实体并带有时间字段

I created the Entity and with Time Field

 /**
 * @var \DateTime
 *
 * @ORM\Column(name="m_01_ch", type="time")
 */
private $m_01_ch;

在我的控制器中,我检索元素,当我这样做时:

In my controller I retrieve the element and when I do:

$val = $myentity->getM01Ch();

我的价值是(在 XDebug 中)

My value is (in XDebug)

$val = {DateTime}[3]
date= "2015-07-08 09:00:00.000000"
timezone_type = 3
timezone "Europe/Rome"

如果我得到 $val->date 我有所有的日期,但我只想得到 09:00:00我可以在不使用 Regex 等的情况下获取我的原始"值吗?

If I get $val->date I have All the Date, but I want to get only 09:00:00 Can I take my "original" value without use Regex etc?

推荐答案

您可以修改返回值的实体.在你的函数 getM01Ch() 中做这样的事情

You can modify your entity where you are returning your value. In your function getM01Ch() do something like this

/**
 * @return \DateTime
 */
public function getM01Ch()
{
    $returnValue = $this->m_01_ch->format('h:i:s') 
    return $returnValue
}

除此之外,我不知道是否存在更好的方法.更多信息

Other than that I don't know if any better approach exists. More info

这篇关于在 Symfony 2 中从数据库中获取时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆