Is there any chance to use Illuminate Query builder to get dates as Carbon objects instead of strings?
For example:
$user=DB::table('users')->select(["id","lastLogin"])->where("id",1)->first();
$user->lastLogin; // <--- Carbon instead of string!
You can use Carbon::parse($user->lastLogin), but I think there is no native way to get dates as carbon objects without using eloquent. An example is this answer on stackoverflow.
I think working with eloquent models will make the work much easier. So you can set the $dates property to get the lastLogin as carbon object. More informations at laravel docs.
Related
I am trying to filter a date using Symfony 5 query builder. But the DateTime object has time in it and the query builder uses the time in the query too.
In my controller, I am catching the POST variables submitted through the form and storing them in an array to pass into my repository.
$criteria['date'] = \DateTime::createFromFormat('d/m/Y', $request->get('date'));
And in my repository, I am building the query as follows.
$qb->andWhere('quotation.date = :date');
$qb->setParameter('date', $criteria['date']);
The problem is the database table column has the data type datetime, so when filtering nothing comes up because the DateTime object created has a wrong time in it.
Is there a way to filter only using the date part in PHP or in the database?
You could replace your strict comparison = by filtering between 00:00:00 and 23:59:59.
However, the answer I would recommend is using beberlei/DoctrineExtensions.
A set of extensions to Doctrine 2 that add support for functions available in MySQL, Oracle, PostgreSQL and SQLite.
One of those function is DATE which cast your datetime into a date to help with comparing datetime as date.
You can install it with composer
composer require beberlei/doctrineextensions
And you just have to configure which function you want to implement in your project in the doctrine.yaml file.
In your case you just need the DATE function :
doctrine:
#...
orm:
#...
entity_managers:
default:
#...
dql:
datetime_functions:
DATE: DoctrineExtensions\Query\Postgresql\Date
And now you should be able to use the DATE function:
$qb->andWhere('DATE(quotation.date) = DATE(:date)');
$qb->setParameter('date', $criteria['date']);
The simplest way is to separate your DateTime object into Date and Time
The easiest way: You put a "!" before the format.
$criteria['date'] = \DateTime::createFromFormat('!d/m/Y', $request->get('date'));
This sets the time to 00:00. Without the exclamation mark, the current time is always taken.
I'm trying to fetch rows based on the "created_at" field that are older than (for example) 2 days.
I'm using Laravel Eloquent. Can somebody help me with this?
You can use Carbon subDays() like below:
$data = YOURMODEL::where('created_at', '<=', Carbon::now()->subDays(2)->toDateTimeString())->get();
You can use the whereDate() eloquent method to run a query against dates.
Model::whereDate('created_at', '<=', now()->subDays(2)->setTime(0, 0, 0)->toDateTimeString())->get();
The now() is a laravel helper function to get a carbon instance of the current date and time.
NOTE: We set the time using the setTime() method so that it starts from the beginning of the day.
Update: It is also possible to use ->startOfDay().
Hello I'm using laravel and I want users to be able to filter based on a created at date in the format (dd/mm/yyy) e.g 23/08/2016
The database save the created_at field as a timestamp like so 2016-08-25 13:28:04.
Is there a way to convert so that the following query is possible:
Model::whereDate('created_at', '=', $filter['created_at']);
Thanks you.
Not sure if this is efficient, but you can use Carbon and whereDate() methods to achieve this.
Model::whereDate('created_at','=',Carbon::createFromFormat('dd/mm/yyyy',$filter['created_at']));
When I used HTML data input it worked without having to convert anything.
I have a field in my database called "deadline" which is of DATE format, and I want to use Eloquent to say that if the deadline field does not match Carbon::now(); but isn't in the future then don't show this row.
How can I achieve this?
Select records where deadline is greater than or equal to today's date() in the Y-m-d format.
Model::where('deadline','>=', date('Y-m-d'))->get();
Laravel works with Carbon for formatting timestamps etc, which you can easily set the deadline column as one of those Carbon objects with a Laravel date mutator allowing you formatting abilities.
But for a select statement, i'd just use the above personally.
I am using this code successfully, but is there a way to run this where I don't need to convert my variable from a string to a number?
I am confused and wondering if I am doing it efficiently because the mysql timestamp datatype is a string but the php timestamp is a number. There should be a date format conversion for the mysql timestamp,no?
date(DATE_ATOM, strtotime($program->start_time))
the date function takes a timestamp long datatype but mysql stores timestamps in a datetime string format. Is there another way to do this where I just convert once?
I don't know why do you need to change the dates in your code.
In Laravel base Model, dates works as Carbon objects. When that fields are stored in database, they're mutated to database date format. That's because date fields have their owns getter/setters in base model.
You don't need to change your custom date fields formats, only declare them in the $dates Model propertie so it can mutate that fields when they are stored in database.
For example, in this User model class the attribute start_time will be mutated from Carbon to MySQL format when it will be saved.
Class User extends Eloquent
{
protected $dates = ['start_time'];
}
You can read more about that here http://laravel.com/docs/4.2/eloquent#date-mutators
I expect that would help you to understand it better.