php - quoted date is subtracting - php

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)."'
";

Related

MySQL Empty Result When Used Variables In The Query

This query works fine:
$sql = "SELECT *
FROM `pricing_data`
WHERE `Label`='BTC'
AND `Timestamp` LIKE '2018-01-19 00:00:%'
LIMIT 1" ;
But this doesn't:
$date = date("Y-m-d ",strtotime("-1 days", time()))." 00:00:";
$to = "BTC" ;
$sql = "SELECT *
FROM `pricing_data`
WHERE `Label`='$to'
AND `Timestamp` LIKE '$date%'
LIMIT 1" ;
Any Solution?
Thanks.
The error is too small to look
there is an extra space in Y-m-d, because of which two spaces created in DateTime you created and query didn't worked.
$date = date("Y-m-d",strtotime("-1 days", time()))." 00:00:";
//..................^ remove space from here --------^ already have here
Check this output for more clarification:- https://eval.in/939013
Note:- Instead of adding hours and minutes manually do with date() itself like below:-
https://eval.in/939015
So finally code need to be:-
$date = date("Y-m-d 00:00:",strtotime("-1 days", time()));
$to = "BTC" ;
$sql = "SELECT *
FROM `pricing_data`
WHERE `Label`='$to'
AND `Timestamp` LIKE '$date%'
LIMIT 1" ;

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

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

Cannot save php datetime in database

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

how to update datetime cell in MySql and 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

Categories