Retrieving results based on timeslots using MySQL - php

I am working on a function of mine that will return queries based on timeslots.
Here what I am trying to accomplish:
Using a start time y-m-d; I grab results that match that time slot. Than I am given a start hour and an end hour (00:00:00) which I want to than compare the earlier results with to only grab the queries whos start hour and end hour are within or on the given start hour and end hour.
My question is can I do this all with one query in PHP PDO (MySQL) ?
start time format in database: Y-m-d
start and end hour format in database: 00:00:00
Suggestions or thoughts?

Based on the discussion we went through you would need a query like the following:
SELECT *
FROM
`tbl_name`
WHERE
`start_time` = '{Y-m-d}' AND
`start_time_hour` >= '{Start H}' AND
`end_time_hour` <= '{END H}'
ORDER BY
`start_time_hour`;

Related

How to add 1 hour to a queried time from the database?

I'm a beginner for php and developing this web application, which users who registered on this site, can be claimed some scores in every one hour. When a user claims at some time, database stores that time as time data type in to user_claim_time column. When that same user tries for his next claim, this php script be able to get his last claim time and add one hour to check if the user really claims in an one hour.
So, my question is how can we add one hour to queried time. I'm using php time(h:i:s) function to store server's current time into the database.
You can do something like this:
SELECT * FROM your_table
WHERE user_claim_time < NOW() - INTERVAL 1 HOUR
However i recommend you to use user_claim_time column in datetime format.
Because time like this '00:00:00' will produce negative output as one hour subtraction can change the date or month as well. For example date like this '2017-08-01 00:00:00'.
So using datetime is the right way i think to properly compare time difference.

filter Unix Timestamp from database

Suppose I want to see the last 24 hours data from my database which is saved as a Unix Timestamp.
So, the process will be:
Get current time.
Get last 24 hours time.
Unix Timestamp ---
current time --- 1439212332
last 24 hours ---- 1439207714
I have tried in many different ways, but all my processes are wrong. Can someone tell me how to solve this problem ?
I am working on PHP and using a MySQL database.
Your query would look something like this:
SELECT * FROM table WHERE mycolumn < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))
This takes the date of 'now', subtracts one day from it and then converts it to a Unix timestamp to use on whichever column you're storing the value in.

MYSQL Group By date + 3 hours

I have a query that counts the "Xp" difference per day from my database, this all works as it should however it groups from midnight-midnight, what I would like to do is group 3am to 3am.
However another issue I think I may have is that my query may not always have the rows being the exact second at 3am due to the fact that it has to run a huge query and retrieve data from another website per user profile, so it should get all data after 3am, but before maybe 4am or something, so it has enough time to get all of the rows.
my current mysql is:
SELECT FROM_UNIXTIME(date, '%Y%m%d') AS YYYYMMDD, MAX(xp)-MIN(xp) AS xp_gain
FROM skills
WHERE userID = '$checkID'
AND skill = '$skill'
AND date >= '$date'
GROUP BY YYYYMMDD
ORDER BY date ASC
The best way to handle this is to add (if you can) another column that is just a DATE (not a DATETIME) and have this field rollover from one day to the next at 3am, (you can to this by subtracting 3 hours from the current time when doing the INSERT).
This gives you a couple of benefits, especially with a large number of rows:
It is much faster to query or group by a DATE than a range of
DATETIME
It will always query the rows at the exact second of 3am,
regardless of how long the query takes.

how to subtract one one column set of dates from another set of dates and finally add all the rows

I have table data with columns start and End. The column start and end have the date and time in this format: 2012-04-06 07:43:05 . I need to do this: End - Start (for each row obviously) and add up all the resulting time. [ only time field subtraction is enough as the date is probably the same for both start and end]
Any easy way to do this?
I would probably approach this by converting the dates to unix timestamp using UNIX_TIMESTAMP() in MySQL, then it is easier to do calculations like these you want.
Something like:
SELECT
SUM(UNIX_TIMESTAMP(`End`) - UNIX_TIMESTAMP(`start`)) AS time_sum
FROM
data
This will get you a sum of all the differences in seconds.
select sum(timediff(`start`, `end`)) as diff_sum
from your_table
you could use datediff to gets days, or timediff to get a time like '01:23:45',
or my favorite unix timestamps that returns number of secounds
UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start) AS sec

MySQL time/date calculation

First, an entry in the database:
I have an input form that writes start date, start and end times (in hour and minute) of working days plus lunch break in minutes (in the example dato=date, modetime=start hour, modeminut=start minute, fyrtime=end hour, fyrminut=end minute). I need to do several calculations:
First calculate the date, start hour and minute into the datetime field modetid.
The do a similar calculation with the end hours and minutes, but move the date up one day if end hours is less than start hour (lets call it fyrtid)
And finally calculate the difference between fyrtid and modetid minus the pause minutes.
Can it be done directly and automatically in the database (if yes, how) or do I need some PHP to do it (and again, if yes, how)?
I know its a tall order but I have not been able to find much information on date/time calculations that made much sense on my low level of knowledge. Any link to a comprehensive guide on date/time calculation in MySQL or PHP would also be greatly welcomed.
I suggest you to work by php function time() it's based on unix timestamp ( like UNIX_TIMESTAMP() in Mysql ) unix time is like this : 1307387678.
Use a calender in your form for start time also for your end time.
put a facility what clients could select time of day ( hour and minutes ) then covert those fileds by strtotime() to unix timestamp like following code ( set date format to mm/dd/yyyy ) :
$startdate = strtotime ( "$_POST['calender_start'] $_POST['hour_start']:$_POST['minutes_start']" );
$enddate = strtotime ( "$_POST['calender_end'] $_POST['hour_end']:$_POST['minutes_end']" );
Change your db table fields to : cpr,startDate,endDate,pause,basked,.....
it's so flexible.
while you want to fetch special recorde you could fetch rows by this sql :
SELECT ...... FROM [TABLE_NAME] WHERE [VALUE] BETWEEN startDate AND endDate
I hope it be usefull for you

Categories