php-carbon

Laravel Collection Date comparison

大憨熊 提交于 2019-12-18 19:09:03
问题 Alright so I have been looking for hours for an answer but can't seem to find one. I have an array of "orders" all for different dates. I get all of the orders for this week, this should not be an issue, then I want to a tab for each day of the week. What I have tried so far is this: $dinnerOrders->where('date','>',$date)->where('date','<', $date->endOfDay())->sortBy('created_at'); Where $date is : $dt = Carbon\Carbon::create()->startOfWeek(); Carbon\Carbon::setTestNow($dt); $date = new

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

心已入冬 提交于 2019-12-18 09:25:47
问题 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)

PHP Carbon, get all dates between date range?

末鹿安然 提交于 2019-12-17 10:20:09
问题 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. 回答1: 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

Laravel Carbon return me 0 results

試著忘記壹切 提交于 2019-12-12 23:48:22
问题 I need to grab this month data for user in database so I write: dd($user->products->where('paid',1)->where('created_at','>=', Carbon::now()->startOfMonth()->toDateString())->all()); and I got 0 results... When I try Carbon::now()->subMonth() its works good but return me last 30 days ... How I can solve this? How to get only this month data ? 回答1: ->toDateString() is causing the query to look for a string value, and not a date . There's two ways to solve this: (...)->where('created_at', '>=',

Laravel 5.3 : Setting time zones based on users location

我只是一个虾纸丫 提交于 2019-12-12 18:47:36
问题 We have small blog application in Laravel 5.3 in which i need some suggestion about setting time stamps on the following features 1.Admin can able to send notifications to all users with current time stamp (server timezone is Asia/Calcutta) user will see all the notifications based on their time zones. 2.Users can add posts along with their current time stamps , when other users see that posts based on their time zones. So what are the changes i need to do on the timezone settings in my

PHP Carbon “month()” method generates wrong DateTime

浪尽此生 提交于 2019-12-12 17:19:30
问题 I just stumbled upon something really weird. I use the "Carbon" package to generate DateTime objects. I use the following code to create a DateTime string for the beginning of September 2016: Carbon::create()->month(9) If I output this using Laravel's "dd()" function, I receive the following output: Carbon\Carbon { +"date": "2016-10-01 10:22:36.000000" +"timezone_type": 3 +"timezone": "Europe/Vienna" } It returns the 1st of October, rather than the 1st of September! It works fine with every

Carbon get current date if variable is null

家住魔仙堡 提交于 2019-12-12 17:08:48
问题 Inside my blade edit form, I have this: <input type="text" name="birth" class="form-control" id="birth" value="{{ \Carbon\Carbon::parse($associado->birth)->format('d/m/Y') }}"> The problem is: if $associado->birth is NULL in database, Carbon is returning current date. What can I do to avoid that? 回答1: You would need to check if the value is null . Furthermore, you could add birth to the $dates array property in your eloquent model. protected $dates = [ 'dates' ]; This will tell the eloquent

A two digit month could not be found Data missing in Laravel

时光毁灭记忆、已成空白 提交于 2019-12-12 11:18:00
问题 I'm having a table where I'm storing the data in date format through datepicker. While storing the date I'm doing something like this: $data['schedule'] = Carbon::parse($request->schedule)->toDateTimeString(); then in model I've defined like following: protected $dates = [ 'schedule', 'created_at', 'updated_at', 'deleted_at' ]; Now while retrieving the date I'm using diffForHumans() something like this: $event->schedule = $event->schedule->diffForHumans(); But it is throwing an error like

Laravel eloquent get model on date

我的未来我决定 提交于 2019-12-12 02:26:35
问题 I want to get all users created on a specified date: // $date is a Carbon instance parsed from command line argument. // I checked it and it is correct. $users = User::where('created_at', '>', $date->startOfDay()) ->where('created_at', '<', $date->endOfDay()) ->get(); But this returns 0 results, whereas in the database there are rows that correspond to that date. What am I doing wrong? 回答1: Carbon doesn't behave like value object (ie. it's not immutable), so this: $date->startOfDay(); $date-

Laravel carbon failed to parse time string

本小妞迷上赌 提交于 2019-12-11 15:57:24
问题 I need to select users with specific age from model User, but have this error: DateTime::__construct(): Failed to parse time string (birth) at position 0 (b): The timezone could not be found in the database part of controller: $users = User::where(Carbon::parse('birth')->diff(Carbon::now())->format('%y'), '>=', 18)->get(); when i use it in view all working fine: @foreach ($userss as $user) <?php $age_year = Carbon::parse($user->birth)->diff(Carbon::now())->format('%y'); ?> @endforeach Thanks