php-carbon

Calculate difference between two dates using Carbon and Blade

*爱你&永不变心* 提交于 2019-11-28 09:40:17
Does anyone know how to pass a given variable instead the Carbon's default parameters ? The documentation of Carbon says: // CARBON SAMPLE $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto'); $dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver'); echo $dtVancouver->diffInHours($dtToronto); // 3 And i want to do something like this in my controller: // EXAMPLE $date = "2016-09-16 11:00:00"; $datework = Carbon::createFromDate($date); $now = Carbon::now(); $testdate = $datework->diffInDays($now); And retrieving that on a Blade template // VIEW ON BLADE <td> {{

Carbon Difference in Time between two Dates in hh:mm:ss format

江枫思渺然 提交于 2019-11-28 06:48:17
I'm trying to figure out how I can take two date time strings that are stored in our database and convert it to a difference in time format of hh:mm:ss. I looked at diffForHumans , but that does give the format I'd like and returns things like after , ago , etc; which is useful, but not for what I'm trying to do. The duration will never span days, only a max of a couple hours. $startTime = Carbon::parse($this->start_time); $finishTime = Carbon::parse($this->finish_time); $totalDuration = $finishTime->diffForHumans($startTime); dd($totalDuration); // Have: "21 seconds after" // Want: 00:00:21 I

Laravel Carbon See if date is in the past

假如想象 提交于 2019-11-28 03:30:15
问题 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

Converting a carbon date to mysql timestamp.

牧云@^-^@ 提交于 2019-11-28 02:46:57
问题 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

Laravel Carbon Data Missing

旧巷老猫 提交于 2019-11-27 19:24:20
In my model I have the following: protected $dates = [ 'start', 'end', 'created_at', 'updated_at' ]; I am using a datetime picker to insert the start and end dates, in this format: 2016-01-23 22:00 Without the seconds. When I do it like this, I get this error: InvalidArgumentException in Carbon.php line 425: Data missing at Carbon::createFromFormat('Y-m-d H:i:s', '2016-01-23 22:00') in Model.php line 3015 If I do include the seconds, it works. The seconds are not important to me, and I do not want to include them in my datetime picker fields. Any way around this so I can still use those fields

PHP Carbon, get all dates between date range?

蓝咒 提交于 2019-11-27 17:58:00
How can I get all dates between two dates in PHP? Prefer using Carbon for dates. $from = Carbon::now(); $to = Carbon::createFromDate(2017, 5, 21); I wanna have all dates between those two dates.. But how? Can only found solutions using strtotime function. As of Carbon 1.29 it is possible to do: $period = CarbonPeriod::create('2018-06-14', '2018-06-20'); // Iterate over the period foreach ($period as $date) { echo $date->format('Y-m-d'); } // Convert the period to an array of dates $dates = $period->toArray(); See documentation for more details: https://carbon.nesbot.com/docs/#api-period .

Laravel 5 Carbon format datetime

痴心易碎 提交于 2019-11-27 13:32:20
问题 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'); 回答1: 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'); 回答2: It easy for Laravel 5 in your Model add

Get UTC from another timezone with Carbon

て烟熏妆下的殇ゞ 提交于 2019-11-27 11:25:34
问题 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)? 回答1: 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'); 来源: https://stackoverflow

How to compare two Carbon Timestamps?

心已入冬 提交于 2019-11-27 06:29:30
I have two timestamps, edited_at which I created and created_at (Laravel's)... In database, both have type timestamp and default value 0000-00-00 00:00:00... But var_dump(edited_at variable) is giving string. While var_dump(created_at variable) is object/Carbon. What is wrong with these timestamps? I have to compare both after converting into integer using format('U'). I can only call this method on Carbon Object. How can I do that? First, Eloquent automatically converts it's timestamps ( created_at , updated_at ) into carbon objects. You could just use updated_at to get that nice feature, or

Laravel Carbon Data Missing

末鹿安然 提交于 2019-11-27 04:20:38
问题 In my model I have the following: protected $dates = [ 'start', 'end', 'created_at', 'updated_at' ]; I am using a datetime picker to insert the start and end dates, in this format: 2016-01-23 22:00 Without the seconds. When I do it like this, I get this error: InvalidArgumentException in Carbon.php line 425: Data missing at Carbon::createFromFormat('Y-m-d H:i:s', '2016-01-23 22:00') in Model.php line 3015 If I do include the seconds, it works. The seconds are not important to me, and I do not