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.
Related
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.
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);
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
This will be done with PHP.
I basically want to get the number of rows that were inserted 30 minutes ago.
I have a time field on my table which is type TIMESTAMP and on update it's set to CURRENT_TIMESTAMP.
The date is stored in this format:
2011-05-27 04:29:17
My query is supposed to look something like this, however i just can't do it
SELECT COUNT(*) FROM mytable WHERE UNIX_TIMESTAMP(time) < '.time().'-1800
Where time() is PHP's function that fetches the UNIX time.
What it should basically do is print me the number of rows inserted from now to 30 minutes ago, but i just can't seem to make it work.
Can somebody help?
Small edit:
Another problem i am seeing is that php's function time() displays the unix time which is UTC. The time stored in mysql is probably GMT i.e whatever my computer's time/timezone is set to.
You can easily get rows stored from now to 30 mins ago by simply using:
SELECT count(*) FROM mytable WHERE `time` >= DATE_SUB(UTC_TIMESTAMP, INTERVAL 30 minute)
Usage of UTC_TIMESTAMP is just an example if you're storing your date/time data as UTC_TIMESTAMP(), you can probably use NOW() if necessary, depends on what you're storing really.
**EDIT**
Removed bad pointers and fixed example :)
Do you really need your computer's timezone to be different than UTC? why not just set it to UTC & save yourself the confusion? If that doesn't work, just use dateadd() on mysql to convert your mysql timestamp to UTC when checking?
My suggestion would be to write a small function to convert the mysql timestamp to your PHP timestamp format & load it into mysql. Then all you need to do is to call tmstamp(time_stamp) instead of time_stamp in your query. You can do the reverse too i.e. Convert PHP's "30 minutes ago" timestamp to mysql format and rerun your query (probably easier).
Usually it's just a formatting issue. It's not standardized across programs.
suppose i have one column in mysql database which is stated last time one equipment is up in h:i:s format (ex: 00:05:11) or 1d21h, means that the equipment is on since 5 min before, what is the best method i can convert this to unix timestamp, say if using php script how? or direct convert last unix timestamp using mysql function query.
actually, i want to calculate for start time for this equipment uptime in unix timestamp where i have one column in mysql startcapture that will be deducted with last column to get start time. so starttime = startcapture - last (this last time that has to convert to unix timestamp based on now() - h:i:s ). but the problem is sometimes the format change from h:i:s to ex: 1d22h, if h:i:s means the equipment is up since ex: 00:05:11 min before and if 1d22h means the equipment already up 1 day 22 hours before.
so the main things here is to convert last column to appropriate unix timestamp.
please help guys, asap.
update:
simplified like this,
i want to display the start time, that can only be calculate by deducting startcapture column with last column,
startcapture column in unix timestamp, while the last column in h:i:s format sometimes in 1d22h format (means the equipment is already up since 1 day 22 hour or since 00:05:11, hours minute seconds)
the only things I want is to convert last column into unix timestamp which is time() - (00:05:11 or 1d22h),
is there any specific function?
If you have the time parsed into variables then you can use mktime to get the timestamp:
http://www.php.net/manual/en/function.mktime.php