Hello in a PHP Script i have the following MySQL Query
$query_players = "SELECT (current_timestamp -`Online_since`) AS `Since` FROM `streams` WHERE `Team` = $temp AND `Online` = 1 ORDER BY `Viewers` DESC";
then i have:
$since = round($obj_players->Since /3600);
As you probably can imagine $since should contain how long the player is already online in hours. Strangely it has wrong results. Its like the time goes faster in MySQL :P
For example after about 15 minutes it already shows "Online since 1 hour" another approximately 30 mins later it already shows 2 hours and so on.
Anyone know what could be wrong? Maybe current_timestamps is the problem?
current_timestamp is not really measured in seconds. So dividing the difference by 3600 doesn't yield hours, but some arbitrary value.
The difference after 18 minutes is 1800 and round(1800/3600) = round(0.5) gives of course 1.
Depending on your real column type use either timediff() or timestampdiff() for your calculation.
Try using TIMESTAMPDIFF:
$query_players = "SELECT TIMESTAMPDIFF(HOUR, NOW(), `Online_since`) AS `Since`
FROM `streams`
WHERE `Team` = $temp
AND `Online` = 1
ORDER BY `Viewers` DESC";
Related
i've been looking around for a way to do this; I found one tutorial that suggested using the dateadd (which i found out is date_add in mysql) function... but I've had no luck with that.
I need to have something that updates the database every 15 minutes or so via a Cronjob... The cron job is already set up (Php file), but i cannot get the DB to update every fifteen minutes.
anyway, this is the code i've got currently--i was really stressing out trying to figure out how to get it to work... so i know what i'm doing doesn't actually work.
UPDATE starinformation SET starOwner = nextOwner, death = 'off', wifi = 'on' WHERE underSiege = 1 AND siegeStart = (current_timestamp)-15
I've also tried doing this
$date=date('-15 minutes'); $sql = "UPDATE starinformation SET starOwner = nextOwner, death = 'off', wifi = 'on' WHERE underSiege = 1 AND siegeStart = ".$date
You can try this.-
<?php
date('Y-m-d H:i:s', strtotime('-15 minutes'));
?>
Please take a look here, how to deal with times using MySQL
Add 2 hours to current time in MySQL?
Do you mean something like
DATE_ADD(NOW(), INTERVAL -15 MINUTE)
The current time minus 15 minutes can be represented by the two functions CURTIME and SUBTIME:
SUBTIME(CURTIME(),'00:15:00.0');
So, based on your posted statement, the modified version is:
UPDATE starinformation SET starOwner = nextOwner, death = 'off', wifi = 'on' WHERE underSiege = 1 AND siegeStart =SUBTIME(CURTIME(),'00:15:00.0')
This might be what you have been looking for:
UPDATE starinformation SET starOwner = nextOwner, death = 'off', wifi = 'on' WHERE underSiege = 1 AND siegeStart = DATE_SUB(NOW(), INTERVAL 15 MINUTE)
Here DATE_SUB subtracts the given date NOW() with given INTERVAL.
If siegeStart of datetime or timestamp data type then
Use
siegeStart = ( current_timestamp - interval 15 minute )
in the update statement.
But the update will only work if the seconds part of the siegeStart also matches.
If you can omit seconds part of your date, then
Use
date_format( siegeStart, '%Y-%m-%d %H:%i' ) =
date_format( current_timestamp - interval 15 minute, '%Y-%m-%d %H:%i' )
Refer To:
INTERVAL clause: INTERVAL expr unit
DATE_FORMAT(date,format)
I have MySQL table:
and I want to add next row (from script in page) with values:
ip: 178.40.12.36
time: 2014-01-22 14:08:04
browser: Google Chrome
browser_version: 32.0.1700.76
platform: windows
country: Slovakia
Question: How to determine in mysql query to insert only if last insert with same identificator (IP+browser+platform) was 30min ago ?
My current insert (pseudo code):
$exist = SELECT *
FROM table
WHERE ip = $ip
AND browser = $browser
AND platform = $platform
if(!$exist) {
INSERT INTO table ...
}
My Idea:
$exist = SELECT ...
...
AND time < $time - 30MIN
Note: How to write this in MySQL?
You may use this as indicator:
SELECT
COUNT(1)
FROM `t`
WHERE `ip` = '$ip'
AND `browser` = '$browser'
AND `platform` = '$platform'
AND `time`>NOW() - INTERVAL 30 MINUTE
-I've replaced time with NOW() for current time, but you may wish to count from your last time value.
It will select records what are newer than 30 minutes, thus, if it's positive, then you don't need to insert new row(s).
Yes, it's easy.
AND time > NOW() - INTERVAL 30 MINUTE
There are many choices like this for date arithmetic.
You could just filter the SELECT for the INSERT:
INSERT INTO `Table` ( ... )
SELECT $ip, $time, $browser, $browser_version, $platform, $country
FROM `Other_Table`
WHERE ip = $ip AND browser = $browser AND platform = $platform AND
time < $time - 30MIN
Now, clearly that syntax won't work exactly, but you get the idea. If the time isn't 30MIN or more ago then it will return 0 records to INSERT.
This will avoid the need of performing a COUNT or EXISTS first; it can be done in one statement.
$newtime = date("Y-m-d H:i:s", time() + 600);
mysql_query("UPDATE rounds SET clock = $newtime WHERE `round`='$CurrentRound' ") or die(mysql_error()); //update DB
This code is failing to add the current time (+10 mins) to the MySQL database.
The cell in the database is datetime format.
I had done this before, but upon rewriting the code, it has stopped working.
You could use
UPDATE rounds SET clock = NOW() + INTERVAL 10 MIN WHERE round = '$CurrentRound'
To set the clock to 10 mins, per the time on the MySQL server.
Alternatively, you need to add ' around the $newTime variable
UPDATE rounds SET clock = '$newtime' WHERE round = '$CurrentRound'
change your query to this:
("UPDATE rounds SET `clock` = '$newtime' WHERE `round` = '$CurrentRound'")
I need to test current time against a datetime from database, if it has been 30 mins then execute code, if not then dont. This is where I am at and I am stuck:
$link = mysqli_connect("hostname", "username", "password", "database");
$q = "SELECT id FROM dwCache ORDER BY id DESC LIMIT 1";
$qu = mysqli_query($link, $q);
while($row=mysqli_fetch_array($qu, MYSQL_ASSOC)){
$id = $row['id'];
$cache = $row['cache'];
$timest = $row['time'];
}
$newTime =
$difference = $timest
if($timest >= )
As you can see towards the bottom I lose it as I am not sure what to do.
$timest returns : 2013-02-01 12:36:01 as the format Y-m-d h-i-s
Apologies on double post, other deleted.
First convert $timest to timestamp
$time = strtotime($timest);
$curtime = time();
if(($curtime-$time) > 1800) { //1800 seconds
//do stuff
}
do it all in sql statement
SELECT id FROM `dqCache` WHERE `time`<DATE_SUB(NOW(), INTERVAL 30 MINUTE);
This will return everything from your table where the time column is before 30 minutes before now.
I like to use unix timestamps in this situation.
$timest = date('u'); // gets the unix timestamp
$q = "SELECT id
FROM `dwCache`
WHERE {$timest} - UNIX_TIMESTAMP(`timestamp_col`) > 1800";
Explanation:
This basically calculates the difference between the current time and the time in the table column. If it's higher than 1800 (30 minutes), it will select the row, and your PHP code will be executed.
Advantages
There are some advantages to using this instead of the PHP check you started doing. You will select fewer rows, thus occupy less memory.
PS:
Thumbs up for using MySQLi !
SELECT id FROM dwCache ORDER BY id DESC LIMIT 1
you'll get only id field, it's the first.
The second, for time converting: MySQL convert datetime to Unix timestamp
Third: you can convert your time string using strtotime function.
I am trying to get the time of the oldest timestamp in the MySQL table "comment" where the following conditions are met:
Loginid of timestamp is equal to $uid.
Timestamps were made within the last hour.
I have tried the code below and it echoes nothing for $minutes.
How can I get $minutes to echo the oldest timestamp that meets the two conditions above?
EDIT: datecommented is a timestamp.
EDIT 2: $minutes echoes out as a blank space. Could this be a PHP formatting issue?
$result = mysql_query('SELECT * FROM comment WHERE datecommented >= NOW() - INTERVAL 1 HOUR AND loginid = "$uid" ORDER BY datecommented ASC LIMIT 1');
$row = mysql_fetch_array($result);
$minutes = $row['datecommented'];
echo '<div>Test '.$uid.' test '.$minutes.' test.</div>';
please add error checking to your sql query, i suspect your sql is failing
or die(mysql_error());
like this
$result = mysql_query('SELECT * FROM comment WHERE datecommented >= NOW() - INTERVAL 1 HOUR AND loginid = "$uid" ORDER BY datecommented ASC LIMIT 1') or die(mysql_error());
at a guess i would expect the " double quotes around "$uid" should be single quotes,
which means you need to change the single quotes around the whole statement to double quotes
like this
$result = mysql_query("SELECT * FROM comment WHERE datecommented >= NOW() - INTERVAL 1 HOUR AND loginid = '$uid' ORDER BY datecommented ASC LIMIT 1") or die(mysql_error());
You should first try your query on MySQL‘s command line or a tool such as PHPMyAdmin, then you will know if the data being returned is how you expect it.
I also suggest to use a SELECT ... UNIX_TIMESTAMP(thefieldyouwantatimestampfrom) ... WHERE ... in case the column is not storing or returning timestamps as numbers.
Well, if the "datecommented" is in unixtimestamp, then of course it doesn't return the minutes, but seconds. As the unixtimestamp is in seconds.
Does your mysql query even work like that? Or is the actual query problematic too?
Is your $row outputting anything? Try print_r($row);.
However simply put, my answer would be, that you are not converting your raw unxitimestamp into minutes.
You can get your seconds to minutes like this: echo ($row['datecommented'] * 60);
And you can get the minute value of your unixtimestamp like this: echo date("i", $row['datecommented']);