Datetime to Integer - mysql

I have one problem... There is a table in my MySQL database that stores to-do list entries inside a JQuery-type calendar.
I had to generate a calendar_id that will generate a reminder once I created timestamp (considered as time I clicked on any of the calender dateboxes, to key in some to-do tasks - put it simple: created datetime).
This to-do list activities app is an external application that I've been working on to integrate with my own management system. I noticed that,the timestamp column is in int(11) format, so whatever timestamp entered will be converted into integer.
For example, take a look at this:
2012-02-22 15:31:24
converted to
1329899400
How can we convert datetime to this format? It's not in seconds when I tried:
intval(floor($datetime/86400));
Any help?

FROM UNIXTIME can format UNIX timestamps into datetime fields:
SELECT FROM_UNIXTIME(time)
FROM ...
The reverse function would be UNIX_TIMESTAMP.
Alternatively you can do it in PHP, if available:
To store a date into the DB format it like this:
$datetimeStr = '2012-02-22 15:31:24';
$datetime = strtotime($datetimeStr);
To retrieve it from the DB and format it to the original format, use something like this:
$dateTimeFromDB = '1329921084';
$datetimeStr = date('Y-m-d H:i:s', $dateTimeFromDB);

Here's a nice MySQL's UNIX_TIMESTAMP() function for you

Related

How can I convert datetime : 1518427800 to hh:mm:ss dd/mm/yyyy in node.js?

I am trying to built a attendance app for my college where in I am fetching data for a web time-table application which has a sql backend and there the lecture start time and end time is in format 1518427800. I am not even sure what format is this.
I am using node.js to fetch from mysql and push it in firebase database, but before i pushing it in firebase. i want to convert the datetime format into something that is understandable.
I am not allowed to alter anything in the web application or its database.
It is a unix time stamp and represents the number of seconds since 1970-01-01.
You can just convert it to a number of milliseconds and pass it to the Date constructor:
var datetime = new Date(1518427800 * 1000);
As mentioned in the above answer, it is a UNIX timestamp
Using moment it is very easy to convert into a readable format
const moment = require('moment')
moment(1553939271155).format("DD/MM/YYYY") // outputs "30/03/2019"
moment(1553939271155).format("DD/MMM/YYYY") // outputs "30/Mar/2019"
moment(1553939037562).format("YYYY/MM/DD") // outputs "2019/03/30"
1518427800 is an epoch timestamp. It is the number of seconds since jan 1 1970.
you can simply convert it into the number of milliseconds
ex. 1518427800*1000
then , pass it to the DATE constructor
ex. var datetime = new Date(1518427800 * 1000);

Convert varchar string data to time format in mysql

I need to convert varchar string data to time format in mysql.
For example,
I have a varchar column in table which stores time. The accepted values should be like 9:30 AM or 1200 PM. But currently it has either blank values or it has values like 9.30am or 12:00
There are many records like this, so cannot update manually.
Ithere any work around or function or procedure to do so?
please help.
Thanks
You can use the STR_TO_DATE() MySQL function to convert any string to a date.
Additionally you can use TIME() to extract the time portion of a datetime. A combination of both function is used to convert an arbitrary date string to a datetime and then you can extract the time portion from it as a valid MySQL TIME.
By default MySQL functions follow standard format but custom format can be specified and if your values don't use the international formats you'll need to check with the documentation and provide the format your system is using.

UNIX_TIMESTAMP outputting NULL in MySQL?

I've got a table setup which has populated data. In column "date", I have dates in the following format:
yyyymmdd i.e. 20131110
I have created a new field and called it newdate with the text format.
Then, I open up the SQL window and put the following in
UPDATE wl_daily
SET
newdate = UNIX_TIMESTAMP(date)
For some reason, it is running correctly, however it only outputs NULL to all the rows. Also, the column name is blank for some reason
Any suggestions?
That's because your field in a string and you're trying to add timestamp to it which is not a string. You need to use a valid datetime field like timestamp for this to work.
Advice: don't store dates and times as strings. Store them in their native format. It makes working with dates and times much easier.
While John Cronde's answer is correct - it doesnt help your situtation
UNIX_TIMESTAMP(STR_TO_DATE(`date`, '%Y%m%d'))
will do the conversion for example
SELECT UNIX_TIMESTAMP(STR_TO_DATE('20131111', '%Y%m%d'))
returns
unix_timestamp(STR_TO_DATE('20131111', '%Y%m%d'))
---------------------------------------------------
1384128000
You should only use this to convert your columns to the date specific columns. Converting each time you need a number will add load and slow down the query if used in production

mysql date format dd/mm/yyyy?

Does anyone know how to insert into column(of date type) date in the format of dd-mm-yyyy?
I'm using calendar, and it selects days like dd-mm-yyyy, but it can't be inserted because of the format that uses mysql by default (yyyy-mm-dd).
The problem is, that I need to compare dates that has been inserted by the calendar and the dates that has been automatically inserted with current_date function. Basicly I have column DoR (date_of_registration) and Status. I've made DoR of date type, and Status of varchar(). In the Status I use Calendar to insert the date. Thus, I've compared it anyway but it gives strange results. I guess it is not comparing correctly.
Any ideas?
Thanks
Convert the output from calendar into date format like this
$cal_date=17-08-2012;
$date=date('Y-m-d',strtotime($cal_date));
echo $date; //gives date as 2012-08-17
Now you can insert this date into mysql and do comparisons
select str_to_date('31-12-2012', '%d-%m-%Y')
See STR_TO_DATE
All you need are these two functions:
STR_TO_DATE()
and
DATE_FORMAT()
Please, always store dates as a DATE type in MySQL. If you need to select it in another form, use the correct string function DATE_FORMAT!
You don't need to insert database as dd-mm-yyyy. You have to user date format. If you are writing code in php you can parse date what every type you want
Select datetime for database. And insert date data date("Y-m-d H:i:s"); when you want to echo date different formats like
echo date('d-m-Y', strtotime('2012-08-17 19:00:00'));
There is also another way to store the date like, if you are choose that
`<input type="date" name="bdate" />
`
then it directly store the date in to database in yyyy-MM-DD Format.
Here my text field format is dd-mm-yyyy.
Here you don't have to convert the string using strtotime()

How to Convert MySQL Date within MySQL Query?

I have a query from within my vb.net code which searchs for a record by date. The date being entered via the code is in format dd/mm/yyyy, i need to search in the database using yyyy/mm/dd?
Ideally i thought there may just be a function within MySQL which will convert this. If not in vb.net to convert the string 01/01/2011 to 2011/01/01?
Thanks
Take a look at str_to_date() function
select str_to_date('01/01/2011','%d/%m/%Y')
So, your search becomes
select * from table where date_field = str_to_date(your_variable,'%d/%m/%Y')