php 将字符串转换为碳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32549221/
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
Convert String to Carbon
提问by smartrahat
I am using Laravel 5.1
我正在使用 Laravel 5.1
Few days ago I used protected $dates = ['license_expire']
in my model to convert the string date to Carbon instances. In HTML the default value in create form for the date was Carbon\Carbon::now()->format('Y-m-d')
几天前,我protected $dates = ['license_expire']
在我的模型中使用将字符串日期转换为 Carbon 实例。在 HTML 中,日期的创建表单中的默认值是Carbon\Carbon::now()->format('Y-m-d')
In order to show alert in home page i used <p>Licence Expired: <b>{{ $employee->license_expire < Carbon\Carbon::now()?'License has expired':$employee->license_expire->diffForHumans() }}</b></p>
为了在主页中显示警报,我使用了 <p>Licence Expired: <b>{{ $employee->license_expire < Carbon\Carbon::now()?'License has expired':$employee->license_expire->diffForHumans() }}</b></p>
Till then diffForHumans() method works fine.
直到那时 diffForHumans() 方法工作正常。
But in that case the edit form's default value also was today's date no matter what was in database(I am using a partial form). To resolve it I change the default value in HTML was NUll. And add another method in my model to show current date in create form.
但在那种情况下,无论数据库中有什么,编辑表单的默认值也是今天的日期(我使用的是部分表单)。为了解决它,我将 HTML 中的默认值更改为 NULL。并在我的模型中添加另一种方法以在创建表单中显示当前日期。
public function getLicenseExpireAttribute($date)
{
return Carbon::parse($date)->format('Y-m-d');
}
After that when i go to home page i have an FatalErrorException
which says Call to a member function diffForHumans() on string
之后,当我进入主页时,我有一个FatalErrorException
说Call to a member function diffForHumans() on string
When I check the date with dd($employee->license_expire)
it become STRING again.
当我用dd($employee->license_expire)
它检查日期时,它又变成了 STRING。
Can anybody tell me how can I convert the string to Carbon in this situation?
谁能告诉我在这种情况下如何将字符串转换为碳?
or
或者
Make my create form's default date as today's date, the edit form's date from database and I can use diffForHumans() to show alert in home page?
将我的创建表单的默认日期设为今天的日期,从数据库中编辑表单的日期,我可以使用 diffForHumans() 在主页中显示警报吗?
回答by Rwd
You were almost there.
你快到了。
Remove protected $dates = ['license_expire']
消除 protected $dates = ['license_expire']
and then change your LicenseExpire
accessor to:
然后将您的LicenseExpire
访问器更改为:
public function getLicenseExpireAttribute($date)
{
return Carbon::parse($date);
}
This way it will return a Carbon
instance no matter what.
So for your form you would just have $employee->license_expire->format('Y-m-d')
(or whatever format is required) and diffForHumans()
should work on your home page as well.
这样它无论如何都会返回一个Carbon
实例。因此,对于您的表单,您将拥有$employee->license_expire->format('Y-m-d')
(或所需的任何格式)并且也diffForHumans()
应该在您的主页上工作。
Hope this helps!
希望这可以帮助!
回答by mgilberties
Why not try using the following:
为什么不尝试使用以下方法:
$dateTimeString = $aDateString." ".$aTimeString;
$dueDateTime = Carbon::createFromFormat('Y-m-d H:i:s', $dateTimeString, 'Europe/London');
回答by li bing zhao
Try this
尝试这个
$date = Carbon::parse(date_format($youttimestring,'d/m/Y H:i:s'));
echo $date;