Time Field Management for AM/PM & 24 Hours using PHP/MySQL - php

I am using two fields in one of my table named as "tbl_TimeTable" in MySQL. Field Names are Start Time and End Time. I need to store Open Time and Close Time using these 2 fields. Now the Problem is Open Time is 09:00:00 AM and End Time is 02:00:00 AM. I can not find my results using this query.
Select TIME_FORMAT(a.End_Time, '%r'), a.Company_ID
from tbl_timetable a WHERE
TIME_FORMAT('15:00:00', '%r') BETWEEN
TIME_FORMAT(Start_Time, '%r') AND TIME_FORMAT(End_Time, '%r')
Table contains:
2 as Company_ID and Start_Time = 09:00:00 and End_Time = 02:00:00

Related

How to get same date, start time and end time form database where date my have multiple times

I want Query in PHP Mysql.
I want to get result like if session_Date is same then it shows like
2021-04-02 only one time and session_start time will be first time means 09:30 and last time means 12:30
that means session_date will display only one time and times will display start time and end time.
so I want below formate
session_date | session_start_time | session_end_time
2021-04-02 | 09:30 | 12:30
2021-04-03 | 09:30 | 10:30
thank you in advance.
You could try:
SELECT
session_date,
MIN(session_start_time),
MAX(session_end_time)
FROM session
GROUP BY session_date
UPDATE:
For your case, you could do the following:
SELECT
session_date,
TIME_FORMAT(MIN(STR_TO_DATE(session_start_time, '%h:%i %p')), '%h:%i %p'),
TIME_FORMAT(MAX(STR_TO_DATE(session_end_time, '%h:%i %p')), '%h:%i %p')
FROM session
GROUP BY session_date;
But I would suggest you store date and time in fields with the respective types, that is session_date as DATE and session_start_time and session_end_time as TIME type.
You can try to group the values. If using eloquent and assuming your model is sessions:
Session::groupBy('session_date')->get();

I want to have a column in MySQL database that automatically calculates difference in DATETIME

In MySQL database, I have the following table that stores work clock in and clock out times. I would like the hours column to auto calculate the DATETIME difference in the database.
ID Clock In Clock Out Hours
1 10:00 17:00 7
2 09:00 16:00 7
3 09:00 15:30 6.5
The SQL statement im using to preview the results is:
SELECT TIMESTAMPDIFF(hour, `clock_in`, `clock_out`) as `difference` FROM records
I just want to know how i can apply this to the hours column in the db to auto populate when records are created.
The simplest way to do what you want is to use a view:
CREATE VIEW v_records AS
SELECT v.*, TIMESTAMPDIFF(hour, `clock_in`, `clock_out`) as `difference`
FROM records;
This conveniently calculates the value when it is used. That ensures that the most recent data is used for the calculation.
If you want to get fancy, you can write a trigger. However, you need to deal with both updates and inserts.

Finding number of times days appear incrementally

I have an application that records user transactions into a database. The data recorded includes: the time a user performed a transaction (e.g. purchasing materials), the day (e.g. 2016-12-23), what the user transacted, etc. I have an admin panel that collects this data and shows the overall statistics. I need to analyse the data such that, for example; if i need to find out all purchases made for that week (i.e.from Sunday to the current day of the week), i select all appearances from the database that have the dates from the immediate previous Sunday until the current day, so that i show a weekly statistics.
Now, since the average is done weekly, i need only to get the current date, that of the previous sunday and the days in between. I know how to get these two but no idea how to get the middle dates depending on the day of the week.
Note that the time i huse is in the format
$today = date("Y-m-d");
and to get the previous sunday i use:
$prevsunday = date("Y-m-d",strtotime("last Sunday"));
I ask how to get the middle days, and how to handle the updated query.
Any suggestions to improve my query?
Assuming MySQL, you can use dayofweek to figure out "previous sunday":
mysql> select curdate(), dayofweek(curdate()),
curdate() - interval (dayofweek(curdate())-1) day AS sunday;
+------------+----------------------+------------+
| curdate() | dayofweek(curdate()) | sunday |
+------------+----------------------+------------+
| 2016-02-23 | 3 | 2016-02-21 |
+------------+----------------------+------------+
1 row in set (0.00 sec)
So you'd have something like
SELECT ...
...
WHERE dateofpurchase BETWEEN
(curdate() - interval (dayofweek(curdate())-1) day)
AND
curdate();

How to match date and time together in mysql/php

Date 1 :- 2014-09-27 10:00:00
Date 2 :- 2014-09-29 11:00:00
This range is stored inside the database and it means that user has given time range for 2 days from 27 to 29 and timing from 27 -> 10:00:00 to 29 -> 11:00:00. This means that user will be available from 10 AM to 11 AM between 27 to 29, 2014.
Now if i pass 2014-09-28 13:00:00 which is in date range and also in time range because user specified the entire day for it as can be seen in the range.
SELECT * FROM TABLE_NAME WHERE Id = $Id AND DATE('$currentDate') BETWEEN DATE(From_DateTime) AND DATE(To_DateTime) AND TIME('$timeHour') BETWEEN TIME(From_DateTime) AND TIME(To_DateTime)
From_DateTime = Date1
To_DateTime = Date2
currentDate = 2014-09-28
timeHour = 13:00:00
Now the problem is that logically the parameter passed are within the range but using the query its not because in TIME its not checking the date, 13 is not between 10 & 11 so its not working. I have tried the DATETIME as well but its not working as giving me error.
I need a way to match date & time both at the same time. Anyone having any suggestion. I am using PHP as programming language.
If the values are stored in DATETIME like this format you mentioned
Date 1 :- 2014-09-27 10:00:00
Date 2 :- 2014-09-29 11:00:00
Then you don't even need all that complexity. Just use normal comparison operators
SELECT * FROM yourTABLE
WHERE startDate >= '2014-09-27 13:00:00'
AND endDate <= '2014-09-29 10:00:00'
Ofcourse you can use your PHP variables instead of the test dates I have there in the query. You can format your PHP values to be in line with MySQL date time format.
If you're expecting large result or large database, I would suggest you use the "BETWEEN".
But I would like to clarify your database schema has 2 fields to compare? Namely "To_DateTime" and "From_DateTime"? If so, I seconded Hanky's answer.

PHP/MySQL - Calender/Setting appointments question

I have to columns, both datetimes. One's for the time the appointment starts and the other is for when the appointment ends.
I need a mysql query that'll basically return available time during the day so I can set appointments without them overlapping.
So if I'm open on Monday from 1pm to 6pm and have an appointment from 2pm to 3pm it'll return two results:
DATE 13:00:00 - DATE 14:00:00 and DATE 15:00:00 - DATE 18:00:00
My solution would be to always have 2 datetimes, one being the start time, one being the end time. That would be the 'cleaner' way in my opinion.
The way to get the open appointments is something like this:
SELECT *
FROM appointments
WHERE end_time = "0000-00-00 00:00:00" (or > in stead of = for all appointments)
Or if you leave it empty something like this:
SELECT *
FROM appointments
WHERE end_time IS NULL (or IS NOT NULL for all appointments)

Categories