Query the DB DATETIME column just by date - php

I have a column in the DB with DATETIME type and is with format yyyy-mm-dd hh:mm:ss. Nevertheless, as information I am given just the date (and I don't care about the time). So how should I query the DB to search that column just by date, even though the value contains both the date and the time?
Update:
Here's the PHP code where I am trying to select the entities:
public static function getTripsWithDrivers($route_from = null, $route_to = null, $date = null)
{
$trips = Trip::whereRouteFrom($route_from)->whereRouteTo($route_to)->whereStartDate($date);
...
}

Use MySql date function:
$trips = Trip::where(DB::raw('DATE(route_from)'), '=', $route_from)
->where(DB::raw('DATE(route_to)'), '=', $route_to)
->where(DB::raw('DATE(start_date)'), '=', $date)

User where filter as the following:
WHERE "datetime column" like '2015-04-06%'

Related

How do I make the unique check on month of a year in Laravel

Here is a table named installments which has date_of_receipt and user_id columns. I need to check if a date date_of_receipt = 27-01-2019 has present according to a user so I have to validate this entity already exists here for any January 2019 day. How can I do that in Laravel5.7 with MySQL?
Because you are storing it as a string in a particular format, you could achieve this with something like this:
$start = \Carbon\Carbon::parse($request->date_of_receipt)->startOfMonth()->format('d-m-Y');
$end = \Carbon\Carbon::parse($request->date_of_receipt)->endOfMonth()->endOfDay()->format('d-m-Y');
return DB::table('installments')
->where('user_id', $user_id)
->whereBetween('date_of_receipt', [$start, $end])
->exists();

Retrieve data from the database using Eloquent between two dates

I'm having trouble retrieving data from the database using Eloquent. I am trying to retrieve all data between two dates (created_at field).
I've done this:
public function search(Request $request){
$start_date = $request->get('start_date');
$end_date = $request->get('end_date');
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('Date_Occured',[$start_date, $end_date])
->get();
dd($getinfo);
}
if you want to select between dates of the media table for example, then fully qualify the field e.g.
$getinfo = DB::table('media')
->join('content_forms', 'media.id', "=", 'content_forms.media_id')
->whereBetween('media.created_at', [$start_date, $end_date]);
->get();
Also make sure the dates passed in the request are in a correct YYYY-mm-dd format

Retrieve records by date laravel

I have several records in a model, and want to be able to retrieve them by date. My url looks like this:
Nov-01-2017/answers and is generated with the following:
/{{$date->format('M-d-Y')}}/answers"
The created_at format looks like: 2017-11-01 16:58:04.
My controller looks like this:
$record = $record->where('created_at', '=', $date)->get();
It won't retrieve the records and I'm sure it's something to do with my date formatting but any advice on how to retrieve records based on the created_at date would be helpful!
Thanks
Have you tried
$record = $record->whereDate('created_at', '=', date('Y-m-d', $date))->get();
You can use:
$record = $record->whereDate('created_at',
Carbon::createFromFormat('M-d-Y', $date)->toDateString())->get();
So you create carbon based on given format and then you change this into valid string in Y-m-d format.

Laravel Eloquent whereDate() with UnixTimestamps

the entries in my database have a custom timestamp field (e.g. 1488446089).
How do I use whereDate() or whereDay() with unix timstamps?
This of course just works for Date Fields.
$posts = Post::whereDay('uploaded_time', '=', '2017-05-10')->get();
To run it correctly use whereDate instead of whereDay
$posts = Post::whereDate('uploaded_time', '2017-05-10')->get();
To get all specific day means (date of month like 1-30)
$posts = Post::whereDay('uploaded_time', '10')->get();//provide date of month
get more detail : https://laravel.com/docs/master/queries#where-clauses
One way to do this is fetching all Posts, and then filtering the results in PHP:
$myDate = '2017-05-10';
$posts = Post::all()->filter(function (Post $post) use ($myDate) {
$uploadedAt = Carbon::createFromTimestamp($post->uploaded_time)->format('Y-m-d');
return $uploadedAt === $myDate;
});
Why? Because if I'm right you are trying to fetch all posts that were uploaded on a certain date. This means that there is a range of timestamps valid. (60 * 60 * 24). With Carbon (comes with Laravel) you can easily convert your timestamps to Y-m-d formatted strings. Those you then compare to the date string of your choice.
Edit
I have no way to test this at the moment, but it should be possible to do this with a query builder after all:
$myDate = Carbon::createFromFormat('Y-m-d', '2017-05-10');
$startOfDayTimestamp = $myDate->startOfDay()->timestamp;
$endOfDayTimestamp = $myDate->endOfDay()->timestamp;
$posts = Post::whereBetween('uploaded_time', [
$startOfDayTimestamp,
$endOfDayTimestamp
])->get();
Here we create 2 timestamps, one for the very start of the date you wish to filter by, and one for the very end. After that, we can use the builder's whereBetween() method and pass the 2 timestamps.
I found this solution here, and simplified a little bit by replacing the setTime() calls with startOfDay and endOfDay methods.

Laravel date format in where condition

I have this code
use Carbon;
$now = Carbon::now()->toDateString();
So i get the date without the time
public funtion test($brandid){
$nbofsale=DB::table('st_nsales')
->where('brand_id', $brandid)
->where('eodate', $now)
->get();
return $nbofsale;
}
The eodate field in the select is datetime. I want to put it in this query as date only and not to change the field type in database.
Any ideas?
If eodate is a timestamp, you don't need to convert it to a date string:
$nbofsale = DB::table('st_nsales')
->where('brand_id', $brandid)
->whereDate('eodata', Carbon::today()->toDateString());
->get();
Another way to get record for today:
$nbofsale = DB::table('st_nsales')
->where('brand_id', $brandid)
->where('eodata', '>', Carbon::now()->startOfDay());
->get();

Categories