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)
Related
I am begginer using PHP and MSQL and I can not filter the results of my table correctly. I want to show me all the fields in the table that keep the condition that there are still two hours for the date and time I have stored.
I used this query but does not work well, because if the current month is 12 and is stored in the DB 1 is not shown, as if even the time stored is less than 2 hours even another day:
putenv('TZ=Europe/Madrid');
$anoE = date("y");
$mesE = date("m");
$diaE = date("d");
$horaE = date("H");
...
$consulta = "SELECT tipo,nombre,descripcion,hora,minuto ,lugar,duracion,fecha,horacero,id,ano,dia,mes,equipo FROM ".$tabla." WHERE ".$anoE." <= `ano` AND ".$mesE." <= `mes` AND ".$diaE." <= `dia` AND ".$horaE." <= `(hora+2)` ORDER BY ano,mes,dia,hora,minuto";
I don't know how i can write the while condition for the query shows fields which remain them two hours beyond the current time (same day)
If your field type is DATETIME (if not, make it) you can use simple query:
SELECT *
FROM table_name
WHERE datetime_field_name BETWEEN DATE_SUB(NOW(), INTERVAL 2 HOUR) AND NOW()
Which will select all records that are between now - 2 hours and now.
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.
I'm adding a custom date string to the database by using;
$date = date("H:i:sa | d-m-Y");
$sql = "INSERT INTO data
(artist, title, presenter, timeplayed)
VALUES('$artist','$title','$presenter','$date')";
However I can't then quite figure out how to get $date back out but only the last 24 hours worth of entries. So between 00:00:00 yesterday and 23:59:59 today.
I've tried:
SELECT * FROM data WHERE timeplayed > DATE_SUB( NOW(), INTERVAL 24 HOUR)
And similar, however it's bringing back data older than 24 hours.
I'm storing the data in timeplayed as varchar. I wonder if that's the reason?
As per above comments you should save the data as proper mysql date format and to get the data of last 24 hours you can use the inerval of day subtracting from NOW()
SELECT * FROM data WHERE timeplayed >= NOW() - INTERVAL 1 DAY;
I think that you should rethink your approach. All of the Date functions in MySQL are geared toward the formats it expects. It would be much easier to do this in the presentation layer as:
echo date('H:i:sa | d-m-Y', strtotime($row['timeplayed']);
This will allow you to use all of the MySQL functions as expected.
Why not try:
$from_date = date("Y-m-d", strtotime("2013-12-11"))." 00:00:00";
$to_date = date("Y-m-d", strtotime("2013-12-12")." 11:59:59";
select * from data where timeplayed <= $to_date and timeplayed >= $from_date
Due to the custom date, your only savior is this query:
$sql = "SELECT * FROM data WHERE timeplayed LIKE '%".date("d-m-Y")."'";
This will yield horrid performance as your table grows...
$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'")
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";