laravel 调用未定义的方法 Carbon\Carbon::getTimestamp()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43227451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:43:45  来源:igfitidea点击:

Call to undefined method Carbon\Carbon::getTimestamp()

phplaravelphp-carbon

提问by Arcubalino

i'm kinda new in Laravel, i'm developing an application, i just went outside and when i get back i suddenly get this error:

我是 Laravel 的新手,我正在开发一个应用程序,我刚出去,当我回来时,我突然收到这个错误:

FatalThrowableError in FileSessionHandler.php line 70: Call to undefined method Carbon\Carbon::getTimestamp()

FileSessionHandler.php 第 70 行中的 FatalThrowableError:调用未定义的方法 Carbon\Carbon::getTimestamp()

i don't know what to do to solve this problem. Kindly help. Thanks :)

我不知道该怎么做才能解决这个问题。请帮助。谢谢 :)

回答by Alexey Mezenin

You need to use getTimestamp()on the Carbon object, so do this if you want to get current timestamp:

您需要getTimestamp()在 Carbon 对象上使用,如果您想获取当前时间戳,请执行以下操作:

Carbon\Carbon::now()->getTimestamp();

回答by Filipe Lopes

You probably aren't calling the method correctly. When in a controller, it has to be called through \Carbon\Carbon::now()->getTimestamp();insted of Carbon\Carbon::now()->getTimestamp();, since it is relative to Laravel's root.

您可能没有正确调用该方法。当在控制器中时,它必须通过\Carbon\Carbon::now()->getTimestamp();insted of调用Carbon\Carbon::now()->getTimestamp();,因为它是相对于 Laravel 的根。

Alternatively, you can also add use Carbon\Carbon;before the Controller's initialization.

或者,您也可以use Carbon\Carbon;在 Controller 的初始化之前添加。

回答by u7823851

I also encountered this question last day,but I now know why,because the php class DateTime public method getTimestamp can't be called! for example like this:

我昨天也遇到了这个问题,但是现在知道为什么了,因为php类DateTime公共方法getTimestamp无法调用!例如像这样:

    $date = new DateTime();
    echo $date->getTimestamp();

you can try that code,if you get error,then you can understand why your laravel error like that! so in laravel,Carbon extends DateTime,Carbon also can't call getTimestamp()! Another question,why Datetime::getTimestamp Cant't be called? I now don't know,It may be a php bug?,my php version is 7.0.4! I change another php 7.0.10。It become OK! hope I can help you! a new message! I restart my computer,so It is also OK,getTimestamp method can be called right

您可以尝试该代码,如果出现错误,那么您就可以理解为什么您的 Laravel 会出现这样的错误!所以在laravel中,Carbon扩展了DateTime,Carbon也不能调用getTimestamp()!另一个问题,为什么不能调用Datetime::getTimestamp?我现在不知道,这可能是一个php错误?,我的php版本是7.0.4!我又换了个php 7.0.10。就OK了!希望我能帮到你!一条新消息!我重新启动了我的电脑,所以也可以,getTimestamp 方法可以正确调用