我正在尝试设置相同的全局laalevel语言环境:
config('app.locale')
Run Code Online (Sandbox Code Playgroud)
与碳合作.
看起来你可以通过使用以下两种方法来实现:
Carbon::setLocale('fr')
Run Code Online (Sandbox Code Playgroud)
要么
setlocale(LC_TIME, 'theLocale');
Run Code Online (Sandbox Code Playgroud)
所以我尝试过使用中间件或提供商,但没有成功.
(为什么这不是laravel的默认功能?)
首先,我检索所有记录,
//get inventory items
$inv = inventory::all();
Run Code Online (Sandbox Code Playgroud)
然后我循环检索记录并修改created_at和updated_at数据,使其更具人性化日期.
foreach($inv as $i){
$i->created_at = date("M d, Y",strtotime($i->created_at));
$i->updated_at = date("M d, Y",strtotime($i->updated_at));
}
Run Code Online (Sandbox Code Playgroud)
但它给我这个错误,
Carbon.php第425行中的InvalidArgumentException:找到意外数据.发现意外数据.找不到分隔符号数据丢失
请问任何想法,帮助,线索,建议和建议?
这是我的模特
namespace App;
use Illuminate\Database\Eloquent\Model;
class inventory extends Model
{
protected $table = "inventory";
protected $primaryKey = "item_id";
public $incrementing = false;
public function profile(){
return $this->belongsTo('App\profile','username');
}
public function inventory_images(){
return $this->hasMany('App\inventory_images','item_id');
}
}
Run Code Online (Sandbox Code Playgroud)
在刀片中,我可以使用
{{ date("M d, Y",strtotime($i->created_at)) }}
{{ date("M d, Y",strtotime($i->updated_at)) }}
Run Code Online (Sandbox Code Playgroud)
它工作得很好.
我使用laravel,我需要从我收到的时间戳创建碳对象.
TimeStamp:' yy-mm-dd HH:mm '
恩.' 2016-12-20 10:26 '
这可能吗 ?
或任何其他解决方案?
当我尝试修改我的资源模型的默认created_at字段的格式时,我收到以下错误:
{
"error":{
"type":"InvalidArgumentException",
"message":"Unexpected data found.
Unexpected data found.
The separation symbol could not be found
Unexpected data found.
A two digit second could not be found",
"file":"\/var\/www\/html\...vendor\/nesbot\/carbon\/src\/Carbon\/Carbon.php",
"line":359
}
}
Run Code Online (Sandbox Code Playgroud)
以下是产生上述错误的代码:
$tile = Resource::with('comments, ratings')->where('resources.id', '=', 1)->first();
$created_at = $tile->created_at;
$tile->created_at = $created_at->copy()->tz(Auth::user()->timezone)->format('F j, Y @ g:i A');
Run Code Online (Sandbox Code Playgroud)
如果我->format('F j, Y @ g:i A')从上面的代码中删除它,它工作正常,但它不是我想要的格式.问题是什么?我的应用程序中的其他地方几乎完全相同的代码,它可以正常工作.
更新:
使用setToStringFormat('F j, Y @ g:i A')不会导致错误,但会返回null.
我刚开始使用Carbon扩展(到目前为止似乎非常甜),但我对这个Carbon::now()功能感到困惑.根据文档,似乎这个函数应该反映用户当前时区的当前时间,但是,我似乎比GMT提前一小时.
也就是说Carbon::now(),2015-01-01 17:26:46当我在太平洋标准时间,它实际上是当前2015-01-01 08:26:46.
我是否必须为所有实例检测并输入用户本地时区?
是什么赋予了?(我很可能对网站如何获得用户当地时间有一个根本的误解)
我现在和Laravel合作了一段时间,我爱上了碳时间戳实例,操作它们并将它们转换成人类可读日期来显示是多么容易.
但是现在我正在开发一个前端与后端完全分离的项目,我无法调用Carbon API方法将日期转换为人类可读的格式.
是否有一个Javascript库执行与Carbon相同的操作,以便我可以在前端模板中使用它并将人类可读的时间格式注入我的视图中.
我有一个主要在CET地区的项目.我在config/app.php中设置了CET,但是base中的所有数据透视时间戳都以UTC时间存储?
如何设置时间戳的"全局"时区?
我做了这个测试:
<?php
$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;
echo "<br />".date('m/d/Y h:i:s a', time());
$mytime = Carbon\Carbon::now();
echo "<br />".$mytime->toDateTimeString();
?>
Run Code Online (Sandbox Code Playgroud)
这是结果:
The current server timezone is: CET
06/09/2016 12:06:04 pm
2016-06-09 11:06:04
Run Code Online (Sandbox Code Playgroud)
tnx Y.
我使用碳来比较2个日期和今天的日期,但是我在一个名为weekday的数据库中还有另一个字段,其中包含以下值:
'MO''TU''我们'
所以我不仅希望按日期搜索和输出,还要按工作日搜索:
public function show($id)
{
$today = Carbon::now();
$weekday = //whatever carbon or something else has to retrieve today's day
$event = Event::with('businesses')
->where('startdate', '<', $today->format('Y-m-d'))
->where('endate', '>', $today->format('Y-m-d'))
//or where ('weekday') = $weekday?
->get();
return view('events.showEvent', compact('event'));
}
Run Code Online (Sandbox Code Playgroud) 我试图created_at在Laravel中的JSON响应中从users表返回datetime.
在我的数据库中,它显示了值,
2016-07-18 00:00:00但是当我尝试返回JSON api时,它会转换为
{
date: "2016-07-18 00:00:00.000000",
timezone_type: 3,
timezone: "UTC"
}
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题.
我想使用以下 laravel 函数在 MySQL 中存储当前日期时间。实际上,我存储的是静态日期而不是我想将当前日期时间存储在created_at,updated_at数据库中的字段。
function insert(Request $req)
{
$name=$req->input('name');
$address=$req->input('address');
$data=array("name" => $name,"address" => $address,"created_at"=>'2017-04-27 10:29:59',"updated_at"=>'2017-04-27 10:29:59');
DB::table('student')->insert($data);
echo "Record inserted successfully.<br/>";
return redirect('/');
}
Run Code Online (Sandbox Code Playgroud) php-carbon ×10
laravel ×8
php ×6
datetime ×3
laravel-5 ×3
laravel-5.2 ×2
mysql ×2
date ×1
eloquent ×1
javascript ×1
laravel-4 ×1