so I want my MySQL Row to delete when it's been 2 hours after being added by said user.
Here's what I have;
$Link = MySQL_Connect // etc
$now = date('Y-m-d H:i:s', time())
$query = "DELETE FROM my_table WHERE time < NOW() - INTERVAL 2 HOUR";
$result = mysql_query($query) or die ('unable to run: ' .mysql_error());
But when I run it, it's deleting every row even if it hasn't been 2 hours.
Incase this helps, My insert query;
$now = date('Y-m-d H:i:s', time());
mysql_query ("INSERT INTO my_table (name, email, time) values ('$Name', '$Email','$now')");
echo 'Proceed';
Try this:
$query = "DELETE FROM my_table WHERE DATE_ADD(`time`, INTERVAL 2 HOUR) < NOW()";
It adds two hours to your column value and if it is less then the current time it will be deleted.
SQL Fiddle
Are your MySQL server and your HTTPD the same machine (I know mine aren't)? 'cause you're inserting your MySQL data with times from the PHP server and then comparing with MySQL times...
Might be better to insert like this:
INSERT INTO my_table(name,email,time) VALUES ('$name','$email',NOW())
at least that will ensure comparing the right times...
And also make sure TIME is defined as a DATETIME, not just a DATE
Related
i want to create a timestamp by which i can know which post is modified when and all. in mysql databse, i made a coloumn called lastmodified, with type as timestamp. now, when i am updating the data in db, i want to update the current timestamp in last modified. how to do so? also could anyone please tell me, if any function exits for comparing these timestamps.
$now = time();
$query = "update storydb set lastmodified = '$now' where story_id = '$story_id'";
mysqli_query($con, $query);
time() returns UNIX Timetamp in integer format e.g. 1223485636.
You want it in 2014-12-10 02:02:36
Use MySQL now() function instead of $now
$query = "update storydb set lastmodified = now() where story_id = '$story_id'";
now() is a MySQL function that returns current Time Stamp (including date).
No, its not unix timestamp that should be used in there, just a normal NOW() should suffice:
$query = "UPDATE storydb SET lastmodified = NOW() WHERE story_id = ?";
$update = $con->prepare($query);
$update->bind_param('s', $story_id);
$update->execute();
To insert current unix timestamp in data base
$time = time();
$qry = 'update db_name' set column_name = $time where condition;
mysql_query($qry);
i have a column in my database for the date a new record was added, it's set with the following code:
$dateset = date("Y-m-d");
$q = "INSERT INTO appts (date_set) VALUES ('$dateset')";
this is working about 80% of the time, is there any reason that randomly it will add the date as "0000-00-00"?
You should check type of field 'date_set'.
if type is DATE you have to use:
$dateset = date("Y-m-d");
if type is DATETIME you have to use:
$dateset = date("Y-m-d H:i:s");
//It is correct, but database push after time 00:00:00 (but it depends on the settings of the database)
//$dateset = date("Y-m-d");
if type is TIMESTAMP you have to use:
$dateset = time();
Try use sql:
$q = "INSERT INTO appts (date_set) VALUES (NOW())";
I think it would the best decision to use
$q = "INSERT INTO appts (date_set) VALUES (NOW())";
is there any reason that randomly it will add the date as "0000-00-00"?
No.
Either $dateset populated some other way or value got updated to 0's later on.
im experiencing a problem where i have
one insert
then sleep(25)
then second insert.
datetime from first insert ALWAYS matches the datetime from second insert.
The problem is that both inserts happen at the same time after sleep. So i placed a condition for the second insert that checks for the first insert but the problem is there. I am using Zend DB select.
Notes:
I have also tried live()->query($sql) and have the same results
$sql = "insert into leads_verify
(customer, lead_id,dt)
values
('2'
,'111'
,'". date("Y-m-d H:i:s")."')";
$queryResult = Db::live()->exec($sql);
if($queryResult <> '5' ){
sleep(24);
}
$sql2 = "insert into leads_verify
(customer, lead_id,dt)
values
('3'
,'222'
,'". date("Y-m-d H:i:s")."')";
$queryResult = Db::live()->exec($sql2);
The datetime of insertion is always the same for both. Any ideas?
Are you using transactions in Zend?
then you need to commit your statements:
Db::live()->commit();
and by the way:
if the dt field is of the type DATE ore DATETIME, you can use Now() instead of '". date("Y-m-d H:i:s")."'
The solution i used was to connect using mysql_query and run the sql that way. Thanks aLL
I have table in mysql database which have field with datatype is datetime.
I want to save the datetime in this minute, for this I use " Now() ", but it does not work,
It just save 000000000000 in databaes.
If you use php, the correct format is:
date("Y-m-d H:i:s");
UPDATE:
Minutes are expressed as i not m
If you've got a timestamp in PHP check out FROM_UNIXTIME()
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
$tstamp = time();
$query = "INSERT INTO `table` VALUES (FROM_UNIXTIME($tstamp))";
INSERT ... (Now(), ...)
without additional quotes around the function Now()
I would use function time() too, then it's easy to output different kind of timestamps with date().
$query = "INSERT INTO tbl VALUES (".time().");";
date("g") this will return 12-hour format of an hour without leading zeros. For more options see http://php.net/manual/en/function.date.php
An example of a PHP script which sets a date in MySQL manually,
<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUES ('DATE: Manual Date', '2008-7-04')”;
mysql_query($query_date) or die(mysql_error());
?>
An example of a PHP script which sets a date in MySQL Automatic,
<?php
$query_date = "INSERT INTO tablename (col_name, col_date) VALUE ('DATE: Auto CURDATE()', CURDATE() )”;
mysql_query($query_date) or die(mysql_error());
?>
try this: set the 'type' of column named 'date_time' as 'DATETIME' and run the following query:
INSERT INTO my_table (date_time) VALUES (CURRENT_TIMESTAMP)
I have a timezone mismatch I need to correct in a table. All dates and times before unix timestamp 1253568477 need to have 5 hours added to their values to make them equal GMT.
I have the following columns...
date (data type date), off by -5 hours
time (data type time), off by -5 hours
timestamp (data type int), this column is the correct time
The reason the timezones are off is because of the way I was setting the values when inserting the rows...
$sql = "insert into email_opens (userid, email, serial, date, time, timestamp) values (
'$userid',
'$opened_by',
'$serial_number',
now(),
now()',
'".gmmktime()."'
)";
The now() value was using the server timezone (eastern) whereas the gmmktime value specified GMT. I have since corrected this query to always use GMT.
Is there a way I can add 5 hours to both time and date columns for those rows where timestamp < 1253568477 in one batch query?
Clarification:
My goal is to update each incorrect row in the table with the correct time by adding 5 hours to each value.
Try with this:
UPDATE mail_opens SET date = DATE_ADD(CONCAT(date, ' ', time), INTERVAL 5 HOUR), time = DATE_ADD(CONCAT(date, ' ', time), INTERVAL 5 HOUR);
And then you probably need this:
<?php
echo date('Y-m-d H:i:s')."<br>";
$addhours = strtotime('+5 hour');
echo $addhours."<br>";
$newdate = date('Y-m-d H:i:s', $addhours);
echo $newdate;
?>
So, using that:
<?php
$addhours = strtotime('+5 hour');
$newdate = date('Y-m-d H:i:s', $addhours);
$sql = "insert into email_opens (userid, email, serial, date, time, timestamp) values (
'$userid',
'$opened_by',
'$serial_number',
'$newdate',
'$newdate',
'".gmmktime()."'
)";
?>
sure, work out the time you corrected the problem so you know only to update records that have a value less than the unix timestamp of your timezone change.
Then update the field and add 60 * 60 * 5 (5 hours in seconds) to those records.
So, your query would be the following:
UPDATE email_opens SET timestamp = (timestamp + 18000) WHERE timestamp < 1253568477;
Cool.