I want to save a datetime from PHP into my database, but I cannot. The datetime value in the database is always set as 0000-00-00 00:00:00. What am I doing wrong?
My code:
$today = new DateTime();
$dt = $today->format('Y-m-d H:i:s');
$sql = "UPDATE wp_posts SET post_date = $dt, post_date_gmt = $dt WHERE ID = $id";
try put quotation marks:
$today = new DateTime();
$dt = $today->format('Y-m-d H:i:s');
$sql = "UPDATE wp_posts SET post_date = '$dt', post_date_gmt = '$dt' WHERE ID = $id";
Simply, datetime is string, not integer.
$today = new DateTime();
$dt = $today->format('Y-m-d H:i:s');
$sql = "UPDATE wp_posts SET post_date = '$dt', post_date_gmt = $dt WHERE ID = $id";
will be works better:)
You can just use,
$dt = date("Y-m-d H:i:S");
$sql = "UPDATE wp_posts SET post_date = '$dt', post_date_gmt = '$dt' WHERE ID = $id";
Instead of using date time from PHP you should use mysql inbuit now() function, no need to write extra line of code in php.
$sql = "UPDATE wp_posts SET post_date = now(), post_date_gmt = now() WHERE ID = $id";
n you can alos set Now() in your table field so you dont need to set as query , when record will insert to update it will automatically set the current date time.
CREATE TABLE tablename
(
fiedlId int NOT NULL,
fieldName varchar(50) NOT NULL,
fieldDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (fiedlId)
)
Cheers!!
why don't you use timestamp function of mysql
for new table
$sql = CREATE TABLE newtb (current_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
or
$sql = CREATE TABLE newtb (current_time DATETIME DEFAULT NOW());
to modify Table
$sql = CREATE TABLE newtb MODIFY current_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
or
$sql = CREATE TABLE newtb MODIFY current_time DATETIME DEFAULT NOE();
Related
This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 1 year ago.
$NOW = new DateTime();
$date = $NOW->format('Y-m-d'); // return 2018-05-17
I want to update date in database to now date.
Query:
$sql = "UPDATE table SET date = $date WHERE id = $id";
But it update time like this => 0000-00-00
The type of this column is DATE
Why? what I have done wrong?
You can try mysql now() function to update date.
$sql = "UPDATE table SET date = now() WHERE id = $id";
Or you should add single qoutes in query
$sql = "UPDATE table SET date = '".$date."' WHERE id = $id";
Simply add your date value in quotes then try. Change your query to :
$sql = "UPDATE table SET date = '$date' WHERE id = $id";
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)."'
";
I want to fetch all records between two dates from database in php. Date format is dd/mm/yyyy hh:mm:ss. example 06/Dec/2016 05:56:15
I'm using following code
$Sdate=date_create($_GET['sdate']);
$start=date_format($Sdate,"d/M/Y H:i:s");
$Edate=date_create($_GET['edate']);
$end=date_format($Edate,"d/M/Y H:i:s");
$sql = "SELECT * FROM `payments` WHERE `customerid` = '".$_SESSION['id']."' AND dateandtime BETWEEN ('".$start."', '".$end."') ORDER BY id DESC";
But this is not working
Thank You in advance for helping me
Try this:
$Sdate = date('Y-m-d H:i:s', strtotime($_GET['sdate']);
$Edate = date('Y-m-d H:i:s', strtotime($_GET['edate']);
$sql = "SELECT * FROM `payments`
WHERE `customerid` = '".$_SESSION['id']."'
AND dateandtime BETWEEN '$Sdate' AND '$Edate'
ORDER BY id DESC";
The correct syntax is:
dateField BETWEEN dateFieldLow AND dateFieldHigh
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
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" );