How to convert string to time in sql query - mysql

I have data in table "2:00 PM-3:00 PM"
Here, I want to print start time based on AM PM.
For example print start time(2:00PM) as number 14:00.
Can you please suggest sql query for this.

Simply you can do it using date_format()
$date = new \DateTime('2:00PM');
echo date_format($date, 'H:i');
// Output: 14:00
But if you're getting this information from the DB you can do it on query-level but the way of doing it depends on which database you're using.
For example with mysql you can do it using DATE_FORMAT().

Related

Mysql convert string to time

Currently, my start_time column was string type.
I want to convert 8:00 AM to 8:00:00 using MySQL.
I have tried like this but it didn't work SELECT STR_TO_DATE('8:00 AM', '%h:%i %p')
Since you are using Laravel, I recommend to use Carbon.
Carbon is an inherited php class from DateTime what makes you able to format times in any way you want and like.
Related to your question:
SELECT STR_TO_DATE("08:00 AM", "%h:%i %p"); // output: 08:00:00
Works fine, so there is something else that causes your problem, but you are giving us not enough information to help you further.
What db are you using?
What version?
what outpout do you get?
etc.

Compare two datetime in angular (one is fetch from mysql database and one is node server)

I want to compare datetime in angular.
for example i have two following date
Registation Start Date : 2019-08-26 12:24:21 from mysql database
I am using Node server as backend
Current Date from node server
serverDate :new Date();
I have converted above serverDate to mysql format using following code
newServerDate : new Date().toISOString().slice(0, 19).replace('T', ' ')
i have send newServerDate to angular which i have used as front end. In angular i have compare above two date as follows
var DateFormMysql = 2019-08-26 12:24:21
var DateFromNode = 2019-08-26 12:24:21
if(DateFromNode <= DateFormMysql ){
console.log('in if');
}
I am stuck on above code, i am not able to compare two datetime variables
P.S : i want to mention here the datetime i get from node server is not correct
For example when i was writting this question,i get following datetime when i console new Date() in angular
Tue Aug 27 2019 13:27:28 GMT+0530 (India Standard Time)
when i console same new Date() in node server i get following result
2019-08-27T08:00:31.301Z
Dont know where i am getting wrong
I would recommend date-fns because you can import only those functions that you need. Maybe moment.js ha change but it used be very big and you needed to import everything
I'd advise you to use Moment.js : https://momentjs.com/
The package on npm is here : https://www.npmjs.com/package/moment
It's a really powerful library that allows you to compare dates.
This way you could do :
import * as moment from 'moment
moment(DateFormMysql).isAfter(moment(DateFromNode)) //=> true/false

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

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

Unparseable date error in Pentaho

I am using Pentaho to insert and update a table in Mysql.
Source database being oracle 11g and destination is Mysql database.
The query for getting max syncronization time from oracle is
SELECT
max(SYNC_TIME) AS LST
FROM Abc_ADM.ORA_SYNC_STATS
where SYNC_TIME is of Timestamp(6) datatype in Oracle in format 01-FEB-70 12.00.00.000000000 AM.
when i use this query and run the job i get error-
could not convert string [${LST}] to date using format [yyyy/MM/dd HH:MM:ss:SS] on offset location 0
unparseable date [${LST}]
What is that i am declaring wrong? please help
Pentaho is asking for a date-format like
yyyy/MM/dd HH:MM:ss:SS
But your Oracle-Output is different:
01-FEB-70 12.00.00.000000000 AM
For Pentaho its a string, no date at all.
It should work by telling Pentaho the Date-Format:
dd-MMM-yy HH.mm.ss
Do this in an input-Step
or by using a select-values ("Meta-data") step after your input
Important:
Type should be "Date" and Format: dd-MMM-yy HH.mm.ss
I can't post screenshots where you could have seen that it works for me.
T [1]: http://i.stack.imgur.com/1AuPW.jpg

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)