how to update datetime cell in MySql and PHP? - php

I am trying to update an event date & time saved in one cell, I got everything right except the date is not updated,. My update works except the event date & time.
$id = $_POST['id'];
$package = $_POST['package'];
$type = $_POST['type'];
$shortdesc = $_POST['shortdesc'];
$vanue = $_POST['vanue'];
$event_start = $_POST['fmt_date'] . ' ' . $_POST['fmt_time'] . 'Y-m-d H:i:s';
$sql="UPDATE event_table SET package='$package', type='$type',
shortdesc='$shortdesc', vanue='$vanue',
event_start= '$event_start' WHERE id ='$id' LIMIT 1";
$result=mysql_query($sql);

// You should properly format the date for validity in insertion
$id = $_POST['id'];
$package = $_POST['package'];
$type = $_POST['type'];
$shortdesc = $_POST['shortdesc'];
$vanue = $_POST['vanue'];
// if date formatted well used this...
$event_start = $_POST['fmt_date'] . ' ' . $_POST['fmt_time'];
//if not format date
$event_start = date_format($event_start, 'Y-m-d H:i:s');
// or if it comes to be a string..
$event_start = strtotime($event_start);
$sql="UPDATE event_table SET package='$package', type='$type',
shortdesc='$shortdesc', vanue='$vanue',
event_start=now() WHERE id ='$id' LIMIT 1";
$result=mysql_query($sql);

MySQL support the datetime format as follows
YYYY-MM-DD HH:MM:SS
So you need to format your datetime variable in PHP accordingly which mysql accept.

I'm pretty sure, that you will find what you search for on this website: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

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 do I update and set all rows in current hour only

I'd like to only change the rows from the current hour on todays date.
// Have the current date
$date = "2019-03-21 17:14:03";
$rel = 8 // like an id.
$sql = "UPDATE graph SET code = '$code' WHERE myDate = '$date' AND rel = '$rel'";
if ($db->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
You can use the DATE_SUB function.
For example:
$sql = "UPDATE graph SET code = '$code' WHERE myDate >= DATE_SUB(NOW(), INTERVAL 1 HOUR)"
This will return all values where myDate is in the past hour.
Use DATE_FORMAT to compute the beginning of the current hour, and compare it against your datetime column. You don't need to pass the date as a parameter, you can get the current date/time with NOW():
UPDATE graph SET code = :code
WHERE myDate >= DATE_FORMAT(NOW(), '%Y-%m-%d %h:00:00') AND rel = :rel
You can do it in this way by ultilising HOUR() function in MYSQL:
$sql = "UPDATE graph SET CODE = '$code' WHERE DATE(myDate) = '$date' AND HOUR(NOW()) = HOUR ('$date') AND rel = '$rel'"

php - quoted date is subtracting

I don't know why this is happening but I have already quoted my date and everytime I run this code, instead of updating dates, it's just subtracting them and returning:
0000-00-00
Here is my code:
$format_date = date("Y-m-d", strtotime($date));
$newinfo = "UPDATE
tickets
SET
`date` = '".mysqli_real_escape_string($connct,$format_date)."'
AND `boolean` = '".mysqli_real_escape_string($connct,$booleancheck)."'
WHERE
`id` = '".mysqli_real_escape_string($connct,$id)."'
";
$update_this = mysqli_query($connct, $newinfo);
EDIT:
Thanks to #Barmar, I used ,instead of AND and it worked.
$newinfo = "UPDATE
tickets
SET
`date` = '".mysqli_real_escape_string($connct,$formatar_data)."',
`boolean` = '".mysqli_real_escape_string($connct,$booleancheck)."'
WHERE
`id` = '".mysqli_real_escape_string($connct,$id)."'
";

Why does this MySQL Update query not work?

Here is my code for updating the date field for all records that have the field WorkoutID = $currentWorkoutID. When I run the query the dates change to 0000-00-00 and not to the current date. How can I fix this? Btw DB::getInstance() executes the query. I think something is wrong with the actual query?
$currentWorkoutID = $_SESSION['GlobalWorkoutID'];
echo $currentWorkoutID;
$date = date("y/m/d");
echo $date;
$sql = "UPDATE workout SET Date = ".$date." WHERE WorkoutID = ".$currentWorkoutID."";
DB::getInstance()->query($sql);
include single quotes ' for date
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
^ ^
i agrre with the answer
$sql = "UPDATE workout SET Date = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
because maybe the type of Date is String ,
hope to help you,thank you
Try this
$sql = "UPDATE workout SET `Date` = '".$date."' WHERE WorkoutID = ".$currentWorkoutID."";
Because date is a reserved keyword

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"";
?>

Categories