MySQL time/date calculation - php

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

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.

Select date stored as Unix Timestamp and compare with given Unix TimeStamp

I am stuck for couple of Days on SQL specific scenario. The scenario is as follows,
I have a table, lets call it traffic which has 2 columns -> date and `vehicle (well many more but those are the two I need to match).
The date column is stored as Unix Timestamp. Now this would have been easy to just compare the current date (obtain from php from time() function) however the trick here is that some of these dates have time attached to them also.
For example if you run strtotime(13-02-2017 13:00) and strtotime(13-02-2017) you will get 2 different results. Basically I only care to match the date and not the time.
So I need some way to select the vehicle and date from the database that are equalled to the current Unix Timestamp but with the trick explained above, so I just need to much the date ONLY if possible.
You can use FROM_UNIXTIME() to convert a timestamp to a datetime, and then use the DATE() function to get the date part of that.
WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
However, this can't use an index, so another way that can make use of an index is to check if it's in a range of timestamps for the current date:
WHERE date BETWEEN UNIX_TIMESTAMP(CURDATE()) AND UNIX_TIMESTAMP(CURDATE()) + 86399
(there are 86400 seconds in a day).
SELECT * FROM traffic WHERE DATE(date) = DATE(CURRENT_TIMESTAMP);

how to calculate 08:30:00 and login [ this field type is date and time ] difference?

I am calculating a difference between check-in time (08:30:00) it's fixed and login time will be fetched from the database. I find the difference between both of them. for eg. The person login 11:40:00 then the output will be shown as you are "3hrs 40min 00sec" late today.
I have tried this code.
in this query, i am converting login field (date and time into only time ).
select DATE_FORMAT(login,'%H:%i:%s')as TIMEONLY
from attendance
where eid='".$eid."'
ORDER BY login DESC limit 1
please give me solution of a time difference.
Use strtotime(string $time) method.
strtotime — Parse any English textual datetime description into a Unix timestamp
Now you can get the difference between two timestamps and then apply normal mathematical operations to convert them into hours, minutes and seconds.

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.

Retrieving results based on timeslots using MySQL

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`;

Categories