I am new in PHP and MySql. I want to delete rows from a table where order_before passed the current date and time. the order_before is date/time type.
<?php
$db = new PDO('mysql:host=localhost;dbname=db','root','root');
if(filter_has_var(INPUT_POST, 'submit')) {
$now = date("Y-m-d H:i:s");
$r = $db->query("SELECT * FROM db WHERE datetime < '".$now."'");
$n = $r->rowCount();
if ($n){
while($o = $r->fetchObject()) {
$db->query('DELETE FROM db WHERE id = '.$o->id);
}
}
}
Maybe something like the above? (untested, and off the top).
Just add the condition in the statement, should be easy enough:
$sql = sprintf("DELETE FROM mytable WHERE order_before > '%s'", date('Y-m-d H:i:s'));
$db->exec($sql);
Related
I am trying to add in values TIMESTAMP to my database but the it is not working. When I delete the timestamp row in my DB, every value adds and gets inserted. Do i need to set $timestamp to something different for my value?
$exam_name = $data["name"];
$exam_points = $data["total_score"];
$exam_grade = NULL;
$release_status = 0;
$exam_comments = NULL;
$exam_ucid = NULL;
$timestamp = NULL;
$sql = "INSERT INTO exams (exam_name, exam_grade, release_status, total_score, exam_comments,
our_ucid, timestamp )
VALUES
('$exam_name', '$exam_grade', '$release_status', '$exam_points', '$exam_comments',
'$exam_ucid', '$timestamp')";
$query = mysqli_query($conn, $sql);
timestamp is a keyword, you have to change field name
e.g. timestamp1
I want to compare two dates with each other to see if 10 minutes has passed.
This is the code that I've got but I can't quite figure out how to do it.
I get the first date from my table (which is saved as a timestamp, example: 2017-03-26 22:33:45) and then I want to compare it with the time that is right now.
$sql = "SELECT saved_time from table1 where email = '$email'";
$stmt = $conn->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch()) {
$savedTime = $row[0];
}
$now = time();
if (/*10 minutes has passed between saved time and now*/) {
echo "Your account is unlocked";
} else if (/*10 minutes hasn't passed*/) {
echo "Your account is locked";
}
Try This code:
$timezone = "Asia/Kolkata";// Select Timezone as of your Preference or MySQL Server Timezone
date_default_timezone_set($timezone);
$sql = "SELECT saved_time from table1 where email = :email";
$stmt = $conn->prepare($sql);
$stmt->execute(array(':email' => $email));
while($row = $stmt->fetch()) {
$savedTime = $row[0];
}
// Uncomment below Line if $savedTime is in MySQL DATETIME Format
// $savedTime = strtotime($savedTime);
$now = time();
if (round(($now - $savedTime) / 60,2) >= 10){
echo "Your account is unlocked";
} elseif (round(($now - $savedTime) / 60,2) < 10){
echo "Your account is locked";
}
if $now = time(); #(this will generate a timestamp)
assuming $savedTime = $row[0]; is a MySQL date field, you can do
$savedTimeTimestamp= date("Y-m-d H:i:s", $savedTime);
...now 10 time 60 seconds is 10 minutes, so
if ($now > ($savedTimeTimestamp+600))
should do the job
If mysql solution is fine for you, then
select * from table T where TIMESTAMPDIFF(MINUTE,.saved_time,NOW()) > 10
If you need it in php, then
$db_time = $row['saved_time'];//as you have stored in timestamp already
$curr_time = strtotime(date('Y-m-d H:i:s'));
$diff = round(abs($db_time - $curr_time) / 60,2);
if($diff > 10)
Update
select now() as curr_time, saved_time from table where COND;
then in php
$diff = $curr_time-$saved_time;
if($diff > (10*60 ))
I am not sure whether this will work. But just an idea.
I have a table (db_dates) with three columns (id, datetime and name_date).
I want to delete one row, when the date is over, such likes this:
//select string time from database
$selectTime ="SELECT datetime FROM db_dates";
$timeSelect = mysqli_query($con,$selectTime);
//today
$today = date('Y-m-d H:i');
printf ("today: %s \n",$today);
//get one date from database
while($rowTime = mysqli_fetch_row($timeSelect)){
$date = new DateTime($rowTime[0]);
$t = $date->format('Y-m-d H:i');
printf ("date: %s \n",$t);
//delete this row, when the date is over
mysqli_query($con, "DELETE FROM db_dates WHERE '".$rowTime[0]."' < '".$today."'");
}
Not working, how do I do that? It is always deleted all data!
I think this will help you.
Because as per your code date will be always less then today's date the way you are getting.
So you should try below code using DATEDIFF().
//select string time from database
$selectTime ="SELECT datetime FROM db_dates";
$timeSelect = mysqli_query($con,$selectTime);
//today
$today = date('Y-m-d H:i');
printf ("today: %s \n",$today);
//get one date from database
while($rowTime = mysqli_fetch_row($timeSelect)){
$date = new DateTime($rowTime[0]);
$t = $date->format('Y-m-d H:i');
printf ("date: %s \n",$t);
//delete this row, when the date is over
mysqli_query($con, "DELETE FROM db_dates WHERE DATEDIFF('".$rowTime[0]."', NOW()) < 0");
}
I have a field on database, the name is Time_message, and I set default for this date is w, d-m-Y H:i:s, and I wanna create some search using BETWEEN and a value just show "d-m-Y" how can i show it to my query??
I bring my code below..
$date1 = "21-08-2015";
$date2 = "d-m-Y";
$query = mysql_fetch_array(mysql_query("SELECT COUNT(*) as jumlahdata FROM message_complaint WHERE (status_message='".$data['status_message']."') AND (time_message BETWEEN $date1 AND $date2)"));
you are conflicting MySQL and MySQLi. Use MySQLi
$date1 = "21-08-2015";
$date2 = "d-m-Y";
$message = $data['status_message'];
$query = mysqli_query($con, "SELECT COUNT(*) as jumlahdata FROM message_complaint WHERE (status_message='$message') AND (time_message BETWEEN '$date1' AND '$date2')");
$row = mysqli_fetch_array($query, MYSQLI_ASSOC);
$con equal to mysqli connection
In my mysql database dateTime is stored in the following form.
00000-00-00 00:00:00
In my php I want to convert it to tiemStamp form like this.
136716425
I tried using
$date2->getTimestamp();
without success, what function should I use to change the format to timestamp?
$sql = "SELECT * FROM temp_user";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$date1 = new DateTime();
$date2 = $row[dateTime];
echo $date1->getTimestamp();
echo $date2->getTimestamp();
}
MySQL has a builtin function for that called UNIX_TIMESTAMP
SELECT UNIX_TIMESTAMP(NOW())
SQLFiddle Demo
UNIX_TIMESTAMP
UPDATE
$sql = "SELECT *,UNIX_TIMESTAMP(dateTime) unx FROM temp_user";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
$var = $row['unx'];
// other codes
}