Laravel change date format in where clause to match Carbon::now() - php

I need to select entries based on dates happening in the future and the
entries contain the date format:
12/30/17
I'm trying to format the date and compare to Carbon::now() timestamp, with no luck.
$now = \Carbon\Carbon::now();
$bookings = DB::table('booking')
->select('booking.*')
->where('booking.uid', '=', Auth::id())
->where(DB::raw("(DATE_FORMAT(booking.date,'%Y-%m-%d 00:00:00'))"), ">=", $now)
->get();

You'll need to use STR_TO_DATE to convert the string.
$bookings = DB::table('booking')
->select('booking.*')
->where('booking.uid', '=', Auth::id())
->where(DB::raw("(STR_TO_DATE(booking.date,'%m/%d/%y'))"), ">=", $now)
->get();
STR_TO_DATE will convert 12/30/17 to 2017-12-30

I don't think you really need to check date format. Also, you have some redundand stuff in the query. Just do this:
Booking::where('uid', auth()->id())->where('date', '>=', now())->get();
And if the date format is really different in some of the rows in the same column, you really need to fix this and instead of making some dirty fix for that.

$bookings = DB::table('booking')
->select('booking.*')
->where('booking.uid', '=', Auth::id())
->where(DB::raw("(DATE_FORMAT(booking.date,'%Y-%m-%d'))"), ">=", $now)
->get();

Related

Laravel: where not expired?

I have a column called expires_at and its a datetime in my MySQL database. I need to fetch records where expires_at hasn't been met, how can I do this?
My current query looks like this
Model::where('user_id', Auth::user()->id)->get();
You can also try this.
use Carbon\Carbon;
use App\Model;
$now = Carbon::now();
Model::where('user_id', Auth::user()->id)
->where('expired_at', '>', $now)
->get();
https://laravel.com/docs/5.6/queries#where-clauses
If you're looking to compare with current date, try:
Model::where('user_id', Auth::user()->id)
->whereDate('expired_at', '>', date('Y-m-d H:i:s'))
->get();

Compare times and dates in laravel 5.4

I need to check if a time is after or before some time string. The database I have been given has times in multiple formats like 00:00:00 and 1970-01-01 07:00:00. Here is what I am trying to do:
data = {some_id:1, time_1:'1970-01-01 07:00:00', time_2:'1970-01-01 07:00:00'}
$results = $this->model->select(
'table.value as value'
)
->where('table.some_id', '=', $data['some_id'])
->where('table.time_1', '=>', $data['time_1'])
->orWhere('table.time_2', '<=', $data['time_2'])
->get();
But, this just returns every single value in the table. How do I compare dates like this when they are not technically dates?
Of course it will return all records.
Because logic of query is not correct.
For Your query sql will be like:
SELECT table.value AS value
FROM table
WHERE
table.some_id = 1
AND
table.time_1 <= '1970-01-01 07:00:00' OR table.time_2 => '1970-01-01 07:00:00'
But it should be:
SELECT table.value AS value
FROM table
WHERE
table.some_id = 1
AND
(table.time_1 <= '1970-01-01 07:00:00' OR table.time_2 => '1970-01-01 07:00:00')
To achieve this do nested where:
$results = $this->model->select(
'table.value as value'
)
->where('table.some_id', '=', $data['some_id'])
->where(function($q) use ($data) {
return $q->where('table.time_1', '=>', $data['time_1'])
->orWhere('table.time_2', '<=', $data['time_2']);
})
->get();
p.s. if it does not return proper response, I recommend to check dates that You request.
This would be the regular query
data = {some_id:1, time_1:'1970-01-01 07:00:00', time_2:'1970-01-01 07:00:00'}
$results = $this->model->select('table.value as value')
->where([
['table.some_id', '=', $data['some_id']],
[function($query) use($data){
query->where('table.time_1', '=>', $data['time_1'])
->orWhere('table.time_2', '<=', $data['time_2']);
}]
])->get();
If you need to transform the date there's a function called date() this transforms timestamps into date with the given format. Ex:
$date = 1497454883;
$date = date('Y-m-d H:i:s',$date);
date will be "2017-06-14 23:00:00"
If you have any questions let me know.

Eloquent: get only the date from timestamp

I want to get all the entries of a specific day for a user.. I don't know how to format the query.
public function getEntry()
{
$entries = Journal::where('id', '=', Auth::id())
->where('created_at', '=', '\Carbon\Carbon::now()->format("l j F Y")')
->get()->first();
return view('home')->with(compact('entries'));
}
I do not know how to format that 'created_at' to match the server time. Any help will be largely appreciated. Thank You.
Use whereDate() to get all entries for the specific day:
->whereDate('created_at', Carbon::today());
Or use whereBetween():
->whereBetween('created_at', [Carbon::now()->startOfDay(), Carbon::now()->endOfDay()])
Or simple where():
->where('created_at', '>', Carbon::now()->startOfDay())
->where('created_at', '<', Carbon::now()->endOfDay())

Laravel - where less/greater than date syntax

This is not showing the correct count. What is the correct syntax ?
$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', 'CURDATE()')->count();
Use a Carbon instance:
$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', Carbon::now())->count();
You can also use the now() helper
$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', now())->count();
Use DB::raw:
->where('datefield', '>', \DB::raw('NOW()'))
We can also try this one. It works for me.
$date = "2020-04-10";
/*
Assumimng DB `login_date` datetime format is "Y-m-d H:i:s"
*/
$from_date = $date.' 00:00:01';
->where('login_date', '>=', $from_date);
By adding Where Clause in the query, we can find the result having
rows after the particular date.
Option-2:
$date = "2020-03-25"; // Format: date('Y-m-d);
$orders = DB::table('orders')
->select('*')
->whereDate('order_datetime', '<=', $date)
->get();
// Here, Table Field "order_datetime", type is "datetime"
// Assuming DB `order_datetime` stores value format like: "Y-m-d H:i:s"
you can make use of whereDate like below:
$query->whereDate('DeadLine', '>', Carbon::now())->count();

Check two timestamps with current date in PHP (Laravel)

I have two timestamps starting_date and ending_date and I need to compare with current time.
I want to do something like this:
$discount_db = Discount::whereActive(1)
->where('starting_date', '<=', $curdate)
->where('ending_date', '>=', $curdate)
->first();
And I want to check this variable. I have an if where I have to check the timestamps, commands and other..
Use Carbon
$curdate = Carbon::now();
$discount_db = Discount::whereActive(1)
->where('starting_date', '<=', $curdate)
->where('ending_date', '>=', $curdate)
->first();
if(count($discount_db)){
//something happen here
}else{
}

Categories