I have following query and data saved in a MySQL database with a timestamp column. I have to display the timestamp as the actual data and time format like 1333007294 it should be displayed as 03-MAR-2012 12:48:14 PM. I have tried to use MySQL using from_unixtime but it displays the datetime like this 3/29/2012 12:00:00 am. I need to get the format like 03-MAR-2012 12:48:14 PM here is my query.
SELECT DATE( FROM_UNIXTIME( `timestamp` ) ) AS timestamp
from vlasteventwdevice
order by 1 desc
Try like this:
FROM_UNIXTIME(`timestamp`, '%d-%b-%Y %r')
You can make the result upper case later in your application code
Related
I want to fetch all datas that corresponds in the chosen date range.
So the problem is that. When theres included time in the data. It can't fetch the required data to be displayed. But when I remove the time on it. It displays really well. What can I do to make it right?
EXAMPLE VALUES:
2018-10-29 01:21:29pm
2018-10-30 01:21:29pm
EXAMPLE VALUES THAT WORKS:
2018-10-29
2018-10-30
My query:
`"SELECT *,SUBSTRING(order_date,1,10) from orders where order_date >='$fromdate' AND order_date <='$todate'"`
Ideal Solution: You will need to change the datatype of order_date from Varchar(500) to Datetime type, using Alter Table command.
Now, it is noteworthy that the MySQL datetime value is in YYYY-MM-DD HH:MM:SS format. So firstly, you will need to change your datetime string to MySQL datetime format string. Otherwise, directly changing the datatype will lead to irreparable loss/truncation of data.
Your datetime value 2018-10-29 01:21:29pm is basically of YYYY-MM-DD HH:MM:SS AM/PM (12 hour format). In terms of format specifiers, it would be: '%Y-%m-%d %h:%i:%s%p'. Complete list of available format specifiers can be seen in MySQL docs.
Firstly, we use Str_To_Date() function to convert all your data into proper Datetime format.
UPDATE orders
SET order_date = STR_TO_DATE(order_date, '%Y-%m-%d %h:%i:%s%p');
Now, next step is simple. Just modify the datatype to datetime:
ALTER TABLE orders
MODIFY COLUMN order_date datetime;
I am trying to convert dates in the format mm/dd/yyyy to the standard date format yyyy-mm-dd using the STR_TO_DATE function. Some fields in my date column are null and some contain a date.
For instance, 8/22/2011 should become 2011-8-22.
When I select my date column, it looks like this:
8/22/2011
8/10/2010
5/12/2012
etc.
I tried using the code
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y')
which filled the column with NULL values. Also tried
UPDATE table SET date = STR_TO_DATE(#date, '%m/%d/%Y')
with same result, although this time I did not get a warning message.
The first one is correct:
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y')
But if the date is not valid (not in %m/%d/%Y format) then it returns NULL
Try executing and then showing warnings. It tells you what is wrong:
UPDATE table SET date = STR_TO_DATE(date, '%m/%d/%Y');
SHOW WARNINGS;
Maybe some dates are not in format %m/%d/%Y (posibly %d/%m/%Y)
I have been using highcharts for datetime-based charts and it needs a timestamp to make the chart.
In my DB I have a DATETIME column and I convert to timestamp when I do the select:
SELECT UNIX_TIMESTAMP(CONVERT_TZ(`datetime`, '+00:00', ##session.time_zone)) * 1000 AS datetime
I have a lot of rows and I think that select may be slow. Should I store the dates as timestamp instead of datetime?
I am trying to store the date in mysql as mm-dd-yyyy.
The following query updates the table stores the date as 0000-00-00
UPDATE `h3`.`newbatch` SET `DateCreated` = '11-08-2013' WHERE
`newbatch`.`BatchID` =
1 AND `newbatch`.`DateCreated` = '2013-11-08' LIMIT 1
I can always use DATE_FORMAT(DateCreated,'%m %d %Y') during select but is there a way to store date in that format.
The datatype of DateCreated is Date.
I am using MySQL.
Thanks
Do not modify the storage format of a date. The format for the date data type is ISO 8601 standard for a reason. You will lose the ability to perform most date functions elegantly (without first converting to the standard date format). You do the formatting when you run a query.
Dates are stored in DB as such: "2011-05-26 11:00:00" in a datetime field. I'd like to find all rows where the date is greater than the first of this month, ie: 2011-05-01 09:00:00
The date formats in the DB can not be changed. What MySQL function can I use to convert the date in the DB to a format that can handle comparison? I'm guessing the best format for "first of the month" is a unix timestamp?
The time values in the dates are always present but only office hours, so from 09:00 to 17:00.
If it's stored in a DATETIME field, just query on WHERE date > '2011-05-01 09:00:00'.
$firstDayOfMonth = date( 'Y-m-d H:i:s', mktime(0,0,0,date('n'),1,date('Y'));
$query = "SELECT * FROM `table` WHERE `date` >= '$firstDayOfMonth'";
Something like this perhaps?
$oDateTime = new DateTime();
$sQuery = "SELECT * FROM table WHERE date >= ".$oDateTime->format("Y-m")."-01 09:00:00";
Compare to the datetime directly:
WHERE date_field >= '2011-05-01 00:00:00'
DATETIME fields can filtered just like integer fields, example:
SELECT * FROM `table` WHERE `date` > '2011-05-01 09:00:00'
In case you really want to convert to Unix timestamps, have a look at the UNIX_TIMESTAMP function.
You can use TIMESTAMPDIFF function in MySQL to compare time,
FROM_UNIXTIME function to format Unix timestamp as date.
More mysql data and time functions and examples are available in MySQL Reference Manual date and time function .
I think that you can solve it, something like this:
firstly, create first day of month in mysql format in php
$firsDay = date('Y-m-d 00:00:00', strtotime(date('Y-m').'-01 00:00:00'));
and then, use it in the query
select * from something where date >= $firsDay