php-carbon

Laravel 5 Carbon global Locale

↘锁芯ラ 提交于 2019-11-30 02:36:22
问题 I'm trying to set the same global locale of laravel which is : config('app.locale') to work with Carbon. It seems like you can do it by using either : Carbon::setLocale('fr') or setlocale(LC_TIME, 'theLocale'); So I have tried using middleware or providers but was not successful. (why is this not a default feature of laravel?) 回答1: So this is my bad, Carbon is actually using the php setlocale(); the Carbon::setLocale('fr') method is only for the ->diffForHumans() method. Notice that the php

Using Carbon to return a human readable datetime difference

非 Y 不嫁゛ 提交于 2019-11-29 20:35:29
I'm using Laravel 4 to create my project. I am currently building the comments section and I want to display how long ago the post was created, kind of like Facebook's '10 mins ago' & '2 weeks ago' etc. I have done a little bit of research and found that a package called Carbon can do this. After reading the Laravel doc's, it says: By default, Eloquent will convert the created_at , updated_at , and deleted_at columns to instances of Carbon , which provides an assortment of helpful methods, and extends the native PHP DateTime class. But when I return a date column that I have created, it doesn

How can I change the timezone of the outputted date in Laravel 4?

人走茶凉 提交于 2019-11-29 17:30:17
I have a database with an entity table and a users table. The entity table has fields for created_at and updated_at . The users table has a timezone field with a default value of America/Toronto . In my view, I can output the date in the format I want with the following code: $entity->created_at->format('M j, Y \\a\\t g:i A') Now, how can I output the date using the timezone field in the users table? Something like ... $entity->created_at->format('M j, Y \\a\\t g:i A', Auth::user()->timezone); I am looking for a simple way to do this with Carbon (since I am using Eloquent in Laravel, which

Laravel Carbon See if date is in the past

為{幸葍}努か 提交于 2019-11-29 10:02:45
I am very confused by this, maybe its something simple I am not seeing. If I want to see if a date is in the past of today I should be able to do something like this? if( $league->date_start <= Carbon::now() ){ $join = false; $message = 'Sorry, the league has already started'; } if I dump out the dates its $league->date_start = 2017-07-31 00:00:00 Carbon::now() = 2017-11-01 16:29:27 $league->date_start is a protected date so its a carbon instance But this doesnt work, if I switch it to $league->date_start >= Carbon::now() it works and wont let me join. I know the "league" start date is in the

Converting a carbon date to mysql timestamp.

﹥>﹥吖頭↗ 提交于 2019-11-29 09:26:22
I have a timestamp variable column in a mysql database. Trying to convert a carbon timestamp to something that I can input there, but Carbon::now() only returns a Carbon object and when I try to use the timestamp string of the Carbon object, it does not register in mysql. public function store(CreateArticleRequest $request){ $input = $request->all(); var_dump($input); // JUST SO YOU CAN SEE $input['published_at'] = Carbon::now(); var_dump($input); // JUST SO YOU CAN SEE Article::create($input); } My first var dump is like so: array (size=4) '_token' => string

Laravel “Unexpected data found” error when trying to change format of Carbon created_at date

无人久伴 提交于 2019-11-29 08:34:49
问题 When I try to modify the format of the default created_at field of my Resource model, I get the following error: { "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 } } Here is the code that produced the above error: $tile = Resource::with('comments,

Carbon - get first day of month

泄露秘密 提交于 2019-11-29 04:58:22
问题 I am using carbon but trying to get the first day of the month so I can run a report from the beginning of the month till the current day. $date = [ 'start' => new \Carbon\Carbon('last month'), 'end' => new \Carbon\Carbon('today') ]; The above code will show todays date back to same date in the previous month. But I want to get from the 1st to now. Is there an easy way to do this like I am above? Cant find anything in the docs. 回答1: Try as $start = new Carbon('first day of this month');

Laravel 5 Carbon format datetime

前提是你 提交于 2019-11-28 22:50:39
I have an array that returns the following date time: $item['created_at'] => "2015-10-28 19:18:44" How do I change the date to M d Y format in Laravel using Carbon? Currently it returns with an error $suborder['payment_date'] = $item['created_at']->format('M d Y'); First parse the created_at field as Carbon object. $createdAt = Carbon::parse($item['created_at']); Then you can use $suborder['payment_date'] = $createdAt->format('M d Y'); It easy for Laravel 5 in your Model add property protected $dates = ['created_at', 'cached_at'] . See detail here https://laravel.com/docs/5.2/eloquent-mutators

Get UTC from another timezone with Carbon

安稳与你 提交于 2019-11-28 18:32:10
How do I get the UTC date with Carbon if I use another timezone? $timestamp = '2014-02-06 16:34:00'; Carbon::createFromFormat('Y-m-d H:i:s', $timestamp)->timezone('Europe/Stockholm'); I create with a Europe/Stockholm timezone. How do I get the UTC date from that (2014-02-06 15:34)? You can change the timezone with this: $timestamp = '2014-02-06 16:34:00'; $date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Europe/Stockholm'); $date->setTimezone('UTC'); You can use createFromTimestampUTC function: $timestamp = strtotime('2014-02-06 16:34:00'); $date = Carbon::createFromTimestampUTC(

Using Carbon to return a human readable datetime difference

谁说我不能喝 提交于 2019-11-28 16:38:44
问题 I'm using Laravel 4 to create my project. I am currently building the comments section and I want to display how long ago the post was created, kind of like Facebook's '10 mins ago' & '2 weeks ago' etc. I have done a little bit of research and found that a package called Carbon can do this. After reading the Laravel doc's, it says: By default, Eloquent will convert the created_at , updated_at , and deleted_at columns to instances of Carbon , which provides an assortment of helpful methods,