Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对整数的成员函数getTimezone()的Symfony调用_Php_Symfony_Datetime_Timezone_Orocrm - Fatal编程技术网

Php 对整数的成员函数getTimezone()的Symfony调用

Php 对整数的成员函数getTimezone()的Symfony调用,php,symfony,datetime,timezone,orocrm,Php,Symfony,Datetime,Timezone,Orocrm,我想将列:created_on的表从DateTime类型转换为Bigint类型将当前日期时间值保存到MySQL数据库表中。 当我试图调用该方法时,从控制器 $smiledMoment->setCreatedValues(); 在我的实体文件中: /** * @param integer $created_on * * @ORM\Column(name="created_on", type="bigint") */ protect

我想将列:created_on的表从DateTime类型转换为Bigint类型
将当前日期时间值保存到MySQL数据库表中。

当我试图调用该方法时,从控制器

$smiledMoment->setCreatedValues();
在我的实体文件中:

     /**
     * @param integer $created_on
     *
     * @ORM\Column(name="created_on", type="bigint")
     */

    protected $created_on;
     /**
     * Set created_on
     *
     * @param integer $created_on
     * @return SmiledMoment
     */
    public function setCreatedOn($created_on)
    {

        $datetime = (array) $created_on;
        $dat = strtotime($datetime['date']);
        //echo $dat->format('d/m/Y');
        //exit;
        $this->created_on = $dat;
        return $this;
    }
    /**
     * Get created_on
     *
     * @return \DateTime
     */
    public function getCreatedOn()
    {
        return $this->created_on;
    }

    /**
     * Set initial value for created_on/modified_on values
     *
     * @ORM\PrePersist
     */
    public function setCreatedValues()
    {

        $this->setCreatedOn(new \DateTime());
    }
从$dat值生成为1517900080(即时间戳值)。
但不幸的是,我得到了一个错误作为

UTCDateTimeType.php第25行中的fatalthrowable错误:调用成员 函数getTimezone()在整数上


首先需要将UNIX时间戳转换为DateTime对象,如下所示:
$dt=new DateTime('@'.$timestamp)

您首先需要将UNIX时间戳转换为DateTime对象,如下所示:
$dt=new DateTime('@'.$timestamp)

有几种方法可以处理这个问题,但是如果我们继续在您的路径上运行,我们将返回一个
DateTime
对象。为此,您需要将
int
转换回时间

/**
 * Get created_on
 *
 * @return \DateTime
 */
public function getCreatedOn()
{
    $date = new DateTime();
    return $date->setTimestamp($created_on);
}

有几种方法可以处理这个问题,但是如果我们继续使用您的路径,我们将返回一个
DateTime
对象。为此,您需要将
int
转换回时间

/**
 * Get created_on
 *
 * @return \DateTime
 */
public function getCreatedOn()
{
    $date = new DateTime();
    return $date->setTimestamp($created_on);
}

如果您真的想将日期作为整数存储在MySql数据库中——实现这一点最合适的方法——是创建自定义原则的映射类型。

要获得一些关于如何做到这一点的线索,请查看doctrine的DateTimeType的默认实现:

如果您真的想将日期作为整数存储在MySql数据库中,最合适的方法就是创建自定义的映射类型。

要获得一些关于如何做到这一点的线索,请查看doctrine的DateTimeType的默认实现:

这看起来像一个
时间戳
,而不是
日期时间
@AmitMerchant是的!i、 因此,您可能希望首先将其转换为
datetime
getCreatedOn
声称它返回
datetime
,但在您的代码中它将返回一个
BigInt
。所以它不能调整时区。@hcoat你能帮我编码吗?这看起来像是
时间戳
,而不是
日期时间
@AmitMerchant是的!i、 因此,您可能希望首先将其转换为
datetime
getCreatedOn
声称它返回
datetime
,但在您的代码中它将返回一个
BigInt
。所以它不能调整时区。@hcoat你能帮我编码一下吗?