delete string date on database - php

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");
}

Related

Update Mysql row where date equal to todays date

I am trying to update a row in my table where the date in a column is equal to today's date.
I have today's date in a variable $currentDate and when I echo this out it is displayed on the screen in the following format
2020-05-08
Which looks same format as the Db table, but I still get the error invalid datetime format.
Below is the code I'm using. Any help please
$currentDate = date("Y-m-d");
$currentTemp = 33;
echo $currentDate;
$sql = "UPDATE weather_station SET currentTemp = $currentTemp WHERE date = $currentDate";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$sql = "UPDATE weather_station SET currentTemp = :temp WHERE date = :currenDate";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":temp", $currentTemp);
$stmt->bindParam(":currenDate", $currentDate);
$stmt->execute();
This works now

How to update column with system date using mysql and php

I tried getdate() function, made a string and converted to date and did an update query but it won't work (I'm new to php)
$date = getdate();
$mydate = $date['mon']."/".$date['mday']."/".$date['year'];
$time = strtotime('$mydate');
$newformat = date('Y-d-m');
$sql = "UPDATE product SET p_date =".$newformat. "WHERE p_id = 2";
It won't update, may be the query is wrong, I just want to update table with the system date.
Like you said in php this is the format
Correct format for a MySQL DATETIME column is
<?php $mysqltime = date ("Y-m-d H:i:s", $phptime); ?>
Try this
$date = date('Y-m-d H:i:s');
mysql_query("INSERT INTO table (datePosted) VALUES ('$date')");
Take a look at the manual.Hope this helps.
date_default_timezone_set('Asia/Kolkata'); // set your timezone
$date = date("Y-m-d H:i:s");
$sql = "UPDATE product SET p_date ='$date' WHERE p_id = 2";
the function should be
date() not getdate()
<?php
$date = date('Y-m-d');
$time = date("H:i:s", time());
echo "$date or $time";
?>
Working Version
<?php
$datetime = date('Y-m-d') . " - " . date(" H:i:s", time());
$sql = "UPDATE product SET p_date = $datetime WHERE p_id = 2"";
?>

filter date from timestamp field in mysql

I have saved rows in my table with custom timezone.Now I want to retrieve data from those table for just today.
So here is what I tried
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
And my row contains date in timestamp format like 2015-12-07 22:42:02
But I get empty result.
Try this:
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE DATE(date) = CURDATE()" );
to convert time according to timezone: ConvertTimeZone
if $today='2015-12-07 22:42:02'; your query will give the result.
$today='2015-12-07 22:42:02';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );
else do pass the today date and next date and retrieve the value as given
$today='2015-12-07';
$next='2015-12-08';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` >= '$today' and `date` <= '$next' " );
for more details refer this Oracle SQL : timestamps in where clause How to compare Timestamp in where clause
You should convert date to timestamp before passing it to mysql:
date_default_timezone_set('America/Los_Angeles');
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = outputs("SELECT * FROM `table` WHERE DATE_FORMAT(date,'%Y-%m-%d')= $today" );

PHP MySQLI DateTime TimeStamps not pulling correctly

OK! So, I have a page where I'm trying to pull a list of records from the database with dates < 30 days from expiration.
Now, the tricky part is - that the date format is in "yyyy-mm-dd hh:mm:ss"
Here's my code:
$Today = date("Y-m-d h:i:s");
if ($result = $mysqli->query("SELECT * FROM table WHERE DATEDIFF(ExpireDate, '$Today') < 30 ORDER BY `StartDate` ASC"))
{
while($row = mysqli_fetch_array($result))
{
//show records
//this type of query usually works when I'm NOT doing stuff with dates
}
mysqli_close($mysqli);
}
When I try to pull just records without any date/time conditions, it gives me the entire list of records, but when I want to see the those conditions, it doesn't display anything.
How do I show the records that are 30 days from expiration?
I SHOULD NOTE - that this is for internal use, only, and that there is no access from the outside world.
(I just wanted something simple for internal records keeping)
I've even tried this:
select * from table where some_date < curdate() - interval 30 day
and failed miserably... thoughts?
Just let the > & < operators take care of it
$Today = date("Y-m-d h:i:s");
$ExpiresFromToday= date("Y-m-d h:i:s", strtotime("+30 days"));
if ($result = $mysqli->query("SELECT * FROM `table` WHERE (`ExpireDate` < '$ExpiresFromToday' AND `ExpireDate` > '$Today') ORDER BY `StartDate` ASC")){
while($row = mysqli_fetch_array($result)){
//show records
}
mysqli_close($mysqli);
}
EDIT
to work with DATEDIFF tell mysql to treat the datetime string as a date:
$Today = date("Y-m-d h:i:s");
if ($result = $mysqli->query("SELECT * FROM `table` WHERE (DATEDIFF(DATE(`ExpireDate`), DATE('$Today')) < 30 AND `ExpireDate` >= '$Today') ORDER BY `StartDate` ASC")){
while($row = mysqli_fetch_array($result)){
//show records
}
mysqli_close($mysqli);
}

MySql delete operation comparing datetime with current date time

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);

Categories