【问题标题】:Fatal error: Call to undefined method DateTime::setTimestamp()致命错误:调用未定义的方法 DateTime::setTimestamp()
【发布时间】:2013-05-15 00:05:31
【问题描述】:

仅在一台特定服务器上出现上述错误 - 而不是在其他服务器上。假设这是一个php版本问题。 以下是触发错误的代码:

//get time interval
    static function get_interval($now, $post_time){
        $datetime1 = new DateTime();
        $datetime1->setTimestamp($now);

最后一行导致了问题。关于如何绕过它的任何想法?

【问题讨论】:

  • 升级到 5.3.0+ ?如果由于某种原因不能,请自己实现该方法(查看该方法手册页中的第二条评论)

标签: php


【解决方案1】:

如果你的 PHP 版本低于 5.3,那么你可以使用这个类来使用函数“setTimestamp”和“getTimestamp”:

<?php

class MyDateTime extends DateTime
{
    public function setTimestamp( $timestamp )
    {
        $date = getdate( ( int ) $timestamp );
        $this->setDate( $date['year'] , $date['mon'] , $date['mday'] );
        $this->setTime( $date['hours'] , $date['minutes'] , $date['seconds'] );
    }

    public function getTimestamp()
    {
        return $this->format( 'U' );
    }
}

$date = new MyDateTime();
$date->setTimestamp( $someTimestamp );

echo $date->format( 'd/m/Y H:i:s' );

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-13
    • 2023-03-21
    • 1970-01-01
    • 2015-01-28
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多