I am making a schedule display system and it shows the schedules of ferries. my problem is if I dont put midnight time in order then query skips midnight time and displays morning instead
EXAMPLE:-
if i put data in order like this
1:30 am
6:30 am
7:30 am
then the times show correctly, but if its not in order
6:30 am
7:30 am
1:30 am
then the query skips the 1:30 am and goes straight to 6:30 to display.
Here is my laravel SQL Query:- if departure time is greater than current time then show the next available one
DB::table('ferries')->groupBy('ferry_name')
->whereTime('departure_time', '>', Carbon::now()->toTimeString())
->whereIn('schedule_type', ['Weekday'])
->where('terminal_name', $terminal_name)->get()->all();
Resukltsets are by nature unsorted yo you need to add a sorting order
This would sort ascending see manual
DB::table('ferries')->groupBy('ferry_name')
->whereTime('departure_time', '>', Carbon::now()->toTimeString())
->whereIn('schedule_type', ['Weekday'])
->where('terminal_name',$terminal_name)->orderBy('departure_time')->get()->all()
Used unique() instead of groupBy() then the orderBy worked not sure why.
$posts = DB::table('ferries')->orderBy('departure_time', 'asc')
->where('departure_time', '>', Carbon::now()->toTimeString())
->whereIn('schedule_type', ['Weekday'])
->where('terminal_name', $terminal_name)->get()->unique('ferry_name')->all();
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 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'm pretty new to programming and can't seem to figure out my mistake here.
I have a calendar setup, every time a user changes a day I make a new row. This way all changes are logged (again new, id imagine there is a better way.) each row has some displayed information but its primary differentiated by a month a day and a year. IE 1 31 2013 .
I need to get the most recent unique row for each day of the month. So if I run a query for 1/31/2013, I need to return only the most recently created row WHERE month= '1' AND etc.
I'm using..
SELECT t.* FROM(SELECT * FROM calendar a
WHERE NOT EXISTS(SELECT * FROM calendar b
WHERE b.day = a.day AND b.lastaltered > a.lastaltered)) t
WHERE t.month = '12' AND t.year = '2013'
From PHP, if that matters.
Now it works fine if a user makes changes slowly. But I found if someone is quick with it like entering multiple days which end up having a very close time stamp ("lastaltered") it doesn't return that row with my current query. I tested this by modifying the time stamp to a later date and it then returned normally. I hope that explains my problem well enough. I'm still not clear as to why altering the time stamp caused the row to return.
Thank you for your time!
- Jer
Ah ... managing time in a database that doesn't understand what time is (and none of them do).
Go here, read the first book - it explains the intricacies of time and the difficulties of using one data type (DateTime) to represent different concepts:
A fixed instant: 10:15am 4 December 2013 UTC
A recurring instant: 10:15am every day, every Tuesday or the last Monday of a month
An instant defined from an anchor: 2 hours from now
A floating interval: 2 hours
An anchored interval: 2 hours from 10:15am to 12:15pm 4 December 2013 UTC
An instant that does not exist: 2:30 am Sunday 5 October 2014 Australian Eastern Daylight Time
An instant that happens twice 1 hour apart: 2:30am Sunday 6 April 2014 Australian Eastern Daylight Time
... and you get the idea.
The cleanest way to handle your problem is to have a ValidFrom and ValidTo DateTime field, when the user creates the row the ValidFrom is set to now and the ValidTo is set to NULL and a trigger executes that sets all the old entries with a NULL ValidTo to now. This will give a complete audit trail and you can get the current entry by querying for the one with the NULL ValidTo.
I am trying to find a way to select the row from database which the dates are between yesterday 5p.m. and today 4:59p.m.
The database are filled with orders and I am trying to make it display all the orders starting from yesterday's 5p.m. till today's 4:59p.m. I have to make it display those entries everyday so that my client is able to know what had been ordered today until the cut-off time.
I had found a few but only display time from 0000 - 23:59 while what I need is 17:00 - 16:59.
Is there any way to do so?
EDIT:
The query that I had so far:
"SELECT date FROM xcart_orders WHERE date between '".strtotime(date("F j, Y", time() - 60 * 60 * 24))."' and '".strtotime(date('F j, Y'))."'"
This will return me result between now and yesterday. E.g. Currently is 12PM, it will return me the results from yesterday 12PM till now(12PM).
HOWEVER, what I want is it will always show me the result from yesterday 5PM till today 4:59PM. This is necessary as my client is doing e-commerce and the cut-off time for delivery is 5PM. Hence, he needs to consolidate the orders from last cut-off time to current cut-off time.
My apologies for forgetting to inform about this; the date in the database is in UNIX time stamp, hence, my query has 'strtotime'.
Sorry causing confusion, my English isn't good.
Regards,
FT
Check this and try
$yesterday= date("Y-m-d", time()-86400)." 00:00:00";
$yesterday2= date("Y-m-d", time()-86400)." 23:59:59";
Change the time if you like
Try this
SELECT x FROM y WHERE date between '2014-05-07 08:00' and date_add('2014-05-07 08:00', INTERVAL 1 DAY)
The result should be every data you select from date 2014-05-07 8am to 2017-05-08 7.59am
Hope it helps. =)