How can I retrieve data from the database when the date > now() - mysql

My purpose is since the time I login my page, I want my web to show how many updated data in the database. My code is like this
$current = $_SESSION['date'];
$query2 = "SELECT * FROM gmaptracker1 WHERE datetime >= '$current'";
When I echo the $current, it showed 27/09/14 : 06:53:24, so the $current is correct, however, when I request the number of database where date>='$current', I get zero, although I have inserted to the database the data with datetime 28/09/14 : 06:53:24 and 29/09/14 : 06:53:24.
Can anyone help me to get out of this, please?

Few things,
It seems like your code is vulnerable to SQL Injection. Just because you retrieve the content of the date from a session, it doesn't mean that it's safe.
Also, why do you need it to be in a session variable? If you always want to retrieve dates bigger than NOW() you can just write your query this way:
SELECT * FROM gmaptracker1 WHERE datetime >= NOW()
The part that caught my attention was the format you're storing the dates.
You said that when you echo'ed $_SESSION['date'] the value was: 27/09/14 : 06:53:24
Now, that does not look like the date format at all. Is your column actually a datetime or timestampcolumn?
If it's a VARCHAR or any other type other than datetime or timestamp, then there's no way for MySQL to know that you're trying to retrieve dates that occur in the future.
If you already have data stored, then it isn't going to be as easy as changing the data type because you already have data, and your data is in the wrong format. The format that MySQL stores datetime information is as follows:
YYYY-MM-DD HH:MM:SS
Based on the comments you left, you don't need the time > NOW(), you need the time when you log in. Now it makes sense why you're storing that time in a variable.
The problem is the format you're storing it.
Since you're using PHP, then you have to store the time this way:
$time = new DateTime();
$_SESSION['date'] = $time->format("Y-m-d H:i:s");

Related

How to perform correct select over unixtimestamp in mysql

I have table with UNIX sql time stamp like 1615582447 in receivedOn.
I am not sure, how to work with this timestamp in case of some interval.
What is the correct way how to perform sql according to user timezone?
My current code is (for Europe/Prague timezone):
FROM_UNIXTIME(receivedOn) > ("2021-03-07T23:00:00.000Z") AND FROM_UNIXTIME(receivedOn) < ("2021-03-2021-03-08T22:59:59.999Z")
But this select return data outside interval.
What is the correct way or...better?
this can#t work in that form, as you have no correct mysql time to compare it with
SELECT FROM_UNIXTIME(1615582447 ) > CONVERT("2021-03-07T23:00:00.000Z", datETIME)
returns true or 1
you should always use mysql conform dates and time, to use in your query, and let the programming language convert it into the right form

Talend could not parse column as timestamp

I need your help in this issue,
I have a talend job which load data from a table to another with a simple tmap.
I called it a mysterious error because it happended just for a specific datetimes
java.sql.SQLException: Could not parse column as timestamp, was: "2009-06-01 00:00:00"
Thousands of rows before the row containing this line doesn't generate the error
When I modify this date 2009-06-01 00:00:00 to another or just changing the day part or the month or even the hour, It goes without error.
the datasource is a mariadb and the destination is a Mysql database
thnks for your help
and this is the part of code which contain the error generated
if (colQtyInRs_tMysqlInput_5 < 6) {
row5.created_at = null;
} else {
if (rs_tMysqlInput_5.getString(6) != null) {
String dateString_tMysqlInput_5 = rs_tMysqlInput_5
.getString(6);
if (!("0000-00-00")
.equals(dateString_tMysqlInput_5)
&& !("0000-00-00 00:00:00")
.equals(dateString_tMysqlInput_5)) {
row5.created_at = rs_tMysqlInput_5
.getTimestamp(6);
} else {
row5.created_at = (java.util.Date) year0_tMysqlInput_5
.clone();
}
} else {
row5.created_at = null;
}
}
Since you provided no further information in
How the source data looks like, e.g. is it a date field or a string field in the source?
Why parsing would happen, this seems to be connected to the source data being a string
How the parsing pattern looks like
I am going to assume a bit here.
1st: I assume you provide a string in the source. Since this is the case, you'd need to make sure that the date in the column is always formatted the same way. Also, you'd need to show us the timestamp format for parsing.
2nd: You said you'd need to change the values of the date for it to work. This seems to me to be an issue with parsing, so for example you have switched by accident the month and day field, e.g. yyyy-dd-mm HH:mm:ss or something alike. Again, this depends on your parsing string.
Since there is often a bit of confusion about this I created a blog post for date handling in Talend which you could consult as well.
This error is due to Timezone, after trying many solutions, I thought about changing the timezone because My Laptop is in UTC and the database timezone is UTC+01 so Talend generate this error in local environment.
Hope it will help someone else

Mysql Timestamp and Var

I have I'm having to workout the days difference between 2 columns one of which is a timestamp and the other being a varchar
timestamp - 2016-01-25 23:55:23 and varchar - 24/12/2015
not the best format for dates, but given that I'm unable to change the columns type is it possible to work out the difference in days between those 2 columns?
Many thanks
Max
The DateTime Class in PHP is very powerful and flexible, specially using the createFromFormat() method to load dates of different formats quite happily. Used together with the ->diff() method you should get what you want.
Here is an example
<?php
$ts = '2016-01-25 23:55:23';
$vc = '24/12/2015';
$tsdate = DateTime::createFromFormat('Y-m-d H:i:s', $ts);
$vcdate = DateTime::createFromFormat('d/m/Y', $vc);
$interval = $vcdate->diff($tsdate);
echo $interval->format('%R%a days');
Alternatively you could get MYSQL to do the heavy lifting and use its STR_TO_DATE() function when you query this odd varchar field
SELECT tsCol, STR_TO_DATE(varcharColumn, '%d/%m/%Y) as otherDate
Then when you see it in PHP it will be converted to a MySQL DATETIME column
Or going even further and getting MySQL to do all the work you could use MySQL's TIMESTAMPDIFF
SELECT TIMESTAMPDIFF(DAY, tsCol, STR_TO_DATE(varcharColumn, '%d/%m/%Y)) as differenceInDays;

Node.js Updating MySql timestamp

So i do a simple query like so:
connection.query("UPDATE workers SET timestamp='"+thedate+"' WHERE id = " + id,function(err,upres){
connection.release();
if(!err) {
console.log('updated record')
console.log(upres);
}
})
console.log reveals the data format as: 2015-04-02 19:29:14
And if i debug the SQL statement, that turns out to be:
UPDATE workers SET timestamp='2015-04-02 21:31:16' WHERE id = 3;
However, when i list the data, the output is:
[{"id":3,"worker":"John Doe","timestamp":"2015-04-01T22:00:00.000Z","duration":30}]
This is way off compared to the time that is being reported?
What is causing this?
You do not know how MySQL is turning your VARCHAR into a date. There are a lot of configuration options. It would be better to use the STR_TO_DATE function to circumvent all of the assumptions. Here is a link to the docs for STR_TO_DATE().
As a side note, I would strongly recommend using prepared statements as a way to safeguard your application against errors and sql injection.
EDITS:
In regards to your questions, the column could be DATETIME, but your value you are assigning is a VARCHAR
'UPDATE workers SET timestamp = ? WHERE id = ?', ['4/2/2015 3:00:00 PM', 3'], [callBackFunction]
Based on what you said about the conversion not working, I am suspicious about the data type for the timestamp column.
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE NAME = 'workers'
A statement like that should give you all of the information about that column. You could also find this in a GUI, if you have access. There are three different date types in MySQL date, datetime, or timestamp. This is most likely a DATE column, that will not be able to hold the time.

Cron job making date database entry

I have a very simple code for a cron job that makes a date entry into an SQL DB:
$qry_cron_test = "INSERT INTO ".$tblprefix."cron_test SET
create_datetime = '".date("Y-d-m H:i:s")."'";
$rs_cron_test = $db -> Execute($qry_cron_test);
The problem is the following:
Between 1st and 12th of every month the date entry is like this - 2014-10-03 07:30:39, which is what i want.
However, when the current date is between 13th and the end of the month, the date entry looks like this - 0000-00-00 00:00:00. Then when 1st comes the entires are all ok again.
I tested this on couple of servers and also locally on Xampp always with the same result.
Any suggestions? What could be possibly wrong?
You have month and day the wrong way around.
$qry_cron_test = "INSERT INTO ".$tblprefix."cron_test SET
create_datetime = '".date("Y-m-d H:i:s")."'";
$rs_cron_test = $db -> Execute($qry_cron_test);
date("Y-m-d H:i:s")
I recommend that, unless you need milisecond information, you always store date information in Unix Timestamp. It is lighter to store, since it is only a integer value, is faster to retrieve and is universal, since it is always based on UTC.
Specially in PHP, converting date information to (time() and strtotime) and from (date()) a unix timestamp is pretty easy. This way no matter where your user is, you can always show correct information in local time with almost no effort.
Wouldn't it be simpler to just do this:
insert into cron_test
create_datetime
values
(current_timestamp)