In my DB I stored the time of the last page refresh in a TIME column. I want to compare the last page refresh to the current time now and find out how many 5 minuet periods have passed?
I was thinking take the last page refresh and subtract it from the current time then divide by 5. But I don't know about how things are formatted.
Help me please!
SELECT *, FLOOR(((UNIX_TIMESTAMP()-time_field)/300)) AS periods FROM table
Im not sure for if UNIX_TIMESTAMP() returns correct timestamp, you will have to check it ;)
EDIT: checked it myself and its working great ;)
Related
Hi I have a MySQL table of Facebook pages (fbpagesfancount) that has the total fan count by day since 01 Jan 2016.
The structure is like this:-
Pageid, Pagename, Updated_Date, Fan_Count
There are a number of specific days that are missing and do not therefore have fancount values due to Facebook API issues.
The days that are missing are usually single days, for example, there is a value for the day before and the day after.
I'd like to create a new table that has a record for every day since 01/01/2016 for each page (750 pages) and then update the days that are missing by averaging the day before and the day after the missing date.
Is this possible using MySQL only or should I write a script in PHP that performs this task and if so, any suggestions on the logic would be helpful.
Any other suggestions on how to tackle this issue would be welcome.
Thanks
Jonathan
Yes, it is possible in SQL only.
No, you should not attempt it as it is more complicated and for a single shot there's no need.
Yes, write a script in any language you know, for instance PHP.
I'm not sure why you even want to create a new table? You could add a flag to your current table saying its an origional count vs an average, and just find the missing numbers and add them in a script.
I'm making a PHP based site designed to display line graphs based on data over time. Where the user selects a time range and gets a graph corresponding to what was selected.
The problem is that to calculate any given point, I need to know the previous record. I have no way of knowing when it was, it may have been an hour or a week before hand, but it could have been a minute.
So is there anyway, from within SQL, to specify a time range and one record before that?
You can do another query that gets the last record before the time range:
SELECT *
FROM yourTable
WHERE time < #start_time
ORDER BY time DESC
LIMIT 1
You can combine this with the original query using UNION.
I need to set the current date and time in static variable.
I need to insert the 50 records into database table. Here,I need to insert the current date and time. Then, I need to set the current date and time of the 50 records are same. I used this date('Y-m-d H:i:s'); format. This format will change every minutes and seconds.
How to I do. Please help me.
$date = date('Y-m-d H:i:s')
then use this variable,for create records. All records will have same time.
It's been a long time, but I found a simple solution. Maybe it's useful for someone (like me) that wants to do the same thing nowadays.
Even when you store the current time/datetime to a variable, the time it's still running, so it changes every second.
I solved it by storing the time() value into a MySQL table (datetime type of course), so the captured time will be stored as it is and stop running and changing every second. Then, when I want to use it, I just make a query from the MySQL table.
It's a simple way (for me) to capture the "now" value and make it stop running, but maybe there's a better way.
I currently have a database which contains last_login and last_logout columns with the datatype set to datetime. When a user logs in and logs out of the system, the time is updated in both of those columns.
I would now like to create a notification that informs the user of when they previously logged into the system. I would like to display the answer in hours, for example (23 hours ago or 128 hours ago).
I just can't figure out how to perform calculations in PHP with the values in each of these columns. Please help.
I apologise if this question is hard to understand.
Do it in mysql
Select TimeStampDiff(HOUR,LastLoginTime,Now()) From MyLoginTable
For instance
SELECT (TIME_TO_SEC(TIMEDIFF(LOGOUT_COLUMN, LOGIN_COLUMN)) / 3600) AS TimeDiff
should do the trick, will return in seconds the difference and then on PHP side you can convert that in minutes or hours.
So I've got a simple query in MySQL that sets a new member's expiration date once they pay their dues:
UPDATE members SET joined=CURDATE(), expires=DATE_ADD(CURDATE(), INTERVAL 1 YEAR), active='1' WHERE id=1000
this query has run 200+ times, normally with the correct result - the current date is put in the joined field, and a year from that date in the expires field. However, in about 10 instances, the expires date has been set to 00-00-0000 with no obvious explanation. I started writing the query to a text file every time to make sure the syntax was correct and I hadn't missed anything - and I didn't - it's exactly that query (with only the ID varying) for every query, those that work, and those that don't.
The only thing I can think here is that there must be an issue with MySQL's DATE_ADD function. Has anyone else experienced anything like this?
UPDATE:
I should add that the joined field is correct with the current date in the cases where the expires date is incorrect.
I'm using MySQL 5.0.81.
There are no triggers.
The table is using MyISAM.
IMPORTANT UPDATE:
I'm an idiot - when I say 11-30-1999 that's not actually what's in the database. I absent-mindedly wrote that, but in fact the database contains the value 00-00-0000 - 11-30-1999 is just how it gets rendered by PHP onto my page. Sorry about that, hopefully that will make this problem less difficult to figure out.
Just a thought.... those "wrong" dates didn't happen to be leap year dates did they.... Feb. 29th for example?
It shouldn't matter, but it may be a bug.