I have a weird issue with datetimes in my database and PHP code.
Carbon::now() currently returns 2018-03-08 01:33:47 which is correct, that is the time on my computer and in my country.
I put a record with the expires_at value at 2018-03-08 02:32:02 which is in 1 hour (or 59 minutes of you want to become technical).
It wasn't displayed in my query, the query that is supposed to select all records where expires_at is before the current time, so non expired records.
Althought, when setting the datetime to 2018-03-09 01:32:28 it displays that it expires in 23 hours? If I post it anything older (say 14 hours after the current datetime) it will be shown as expired?
$keys = UserAccountKey::where('user_id', Auth::user()->id)
->whereDate('expires_at', '>=', Carbon::now())
->get();
The sql of your query is:
select * from `user_account_keys` where date(`expires_at`) >= '2018-03-08 01:33:47'
The date('expires_at') gets the date part of the expires_at field, ie '2018-03-08', obviously '2018-03-08' is less than '2018-03-08 01:33:47', so Your query did not get any result.
Just use where('expires_at', '>=', Carbon::now()) instead.
See this demo https://implode.io/vTWTWP
Related
I want to filter available time for today for a user. He can only make an appointment for a time slot that is bigger than time right now + 6 hours. But the problem is when + 6 hours transfers to the next day so it could be like 00:44 next day and in that case when it compares to the time slots it shows that time slot at 8:00 for example is bigger than 00:44 so it shows that time slot for today although it shouldn't be possible to make an appointment for that time slot anymore. Is there a way to filter it in the eloquent? I basically want addHours to stop if it reaches 23:59. My Eloquent function:
$times_today = TimeSlot::where('status', 1)
->whereTime('time', '>', Carbon::now()->addHours(6))
->get();
Carbon provides a tomorrow() function that you can use in the same way you've made use of now().
I assume you don't want people booking appointments up until midnight (although perhaps you do?) so I would also use the subHours() function to limit timeslots to before a certain time.
$times_today = TimeSlot::where('status', 1)
->whereTime('time', '>', Carbon::now()->addHours(6))
->whereTime('time', '<', Carbon::tomorrow()->subHours(2))
->get();
You can create timestamp for specfic time using create function with time zone then you can aad hours in current timestamp by using Carbon::now()->addHours(6)
$dtToronto = Carbon::create('2022','8','2', 23,59,59, 'America/Toronto');
$times_today = TimeSlot::where('status', 1)
->whereTime('time', '>', Carbon::now()->addHours(6))
->whereTime('time', '<=', $dtToronto)->get();
I am trying to delete all entries which have not been viewed in the past 30 days and are were created more than 30 days ago.
The column last_viewed_at's default value is null, so in case the entry was created_at 5 days ago and was not viewed, the row should get not be deleted.
I currently use
$medias = Media::where('last_viewed_at', '<=', $delete_after_x_days)->get();
This code snippet does not include the scenarios of where a media item was created during the last 30 days and has not been viewed, so should not be deleted just yet.
Somehow, I need to check created_at if last_viewed_at is null to make sure the media is older than 30 days.
Any help would be greatly appreciated!
i have no idea in your model how you use but your where condition would look like this,
last_viewed_at is not null and last_viewed_at<= $delete_after_x_days
Finally, I came up with a solution thanks to #knowledge....'s hint.
$medias = Media::where('created_at', '<=', $delete_after_x_days)
->where(function ($medias) use ($delete_after_x_days) {
$medias->where('last_viewed_at', '<=', $delete_after_x_days)
->orWhereNull('last_viewed_at');
})
->get();
This makes sure the entry is older than x days and that last_viewed_at is also older than 30 days or is null.
I want to select all the data on a database using laravel db query or Eloquent ORM. So i want all the data that are other than 3 days from the day they where created. Data 'A' is created on 15th i come in on 16th to check, i won't see Data 'A' but i may see others older than 3 days or equal to 3 days. If i come back on the 18th or 19th i should see Data 'A', obviously because it now older than 3 days or equal to.
so i wrote this code that is not working below
$users = DB::table('matched_users')->where( 'created_at', '>=',
Carbon::now()->subDays(3));
so can anyone correct this for me.
when i did that it work but it shows both someone that his date is just one day one. so is that not enough code
Data Name - created_at
ozil 21/04/2012 16:09:22
mark 21/04/2012 16:09:22
cyril 22/04/2012 16:19:21
so today is 25/04/2012 if run the query of run the above query as it is i get all the result back.
But if change the >= to <= the result is an empty collection
I guess you mean 'older than 3 days from the day they where created'. In this case the query should look like this:
DB::table('matched_users')->where('created_at', '<=', Carbon::now()->subDays(3));
$users = DB::table('matched_users')->whereDate( 'created_at', '<=', Carbon::now()->subDays(3))->get();
is the right way to write the code
I have a deliverables table and i want to get results where difference between current date time is 2 days from created date.
e.g today is 4-19-2017 i want to get all records where deadline is 4-21-2017 because here is difference of 2 days between 4-19-2017 and 4-21-2017 or also if 4-20-2017 it should also retrieve how ever if difference is more than 2 it should not be retrieve.
In short if Difference between current date and deadline is 2 days or less should be retrieve
I tried as
$deliverables_dead = DB::table('deliverables')->where('user_id' , Auth::user()->id)->where('deadline' , '>' , Carbon\Carbon::now())->get();
its get all records where is deadline greater than current time.
Please help how can i get answer of above bold statement......
Use whereBetween() and Carbon's addDays():
DB::table('deliverables')
->where('user_id', auth()->id())
->whereBetween('deadline' , [Carbon\Carbon::now(), Carbon\Carbon::now()->addDays(2)])
->get();
I have DB table that logs request with an IP column and a DateTime stamp. I'm trying to fetch this data in a way that makes me count the number of days a certain IP has made requests. I'm using Laravel's query builder.
So far, this is what i've got:
$data = DB::table('requests')
->groupBy('ip')
->select('ip',
DB::raw('COUNT(DISTINCT created_at) as days'),
DB::raw('COUNT(*) as requests'))
->orderBy('days', 'desc')
->take(50)
->get();
My problem is that the timestamp also holds hours, minutes and seconds. So the "days" count will be about the same as the number of total requests. I want to only count the number of days active.
If field created_at is TIMESTAMP:
COUNT(DISTINCT FROM_UNIXTIME(created_at, '%Y-%m-%d')) as days
or if field is DATETIME:
COUNT(DISTINCT DATE(created_at)) as days
You can probably use DATE_FORMAT to do what you want.
Take a look at this question:
Selecting a distinct date by day only from datetime (YYYY-MM-DD HH:MM:SS) in MySQL