Why I can't store a timestamp in mysql / phpmyadmin? - mysql

I have a table created in phpMyAdmin and it contains two fields: start_time and text_modified. It looks like this
so the start_time might be null.
When I'm filling the data in phpmyadmin I can choose the date and time that should be represented as this timestamp:
After doing so I expect to store a timestamp value in this field instead of date time. But when I do a query SELECT start_time from table I see there this:
So I assumed that it is just the php my admin that shows me automatically all dates as a date time value instead of timestamps. But now when I do a query: SELECT FROM_UNIXTIME(start_time) FROM table I'm getting those results:
and instead I want normal dates here. What is going wrong here?

In a timestamp you can insert datetime values, that are internally stored as integers (the seconds since 1970-01-01 as you probably know). When you select them, they are displayed as date and time.
So far so good.
When you have values like 0000-00-00 00:00:00 you probably inserted NULL values or invalid dates or dates out of range for the integer value. Using FROM_UNIXTIME() doesn't make sense here, since this function calculates a date and time value from an integer value. This integer value of the timestamp column is like I said only used internally. Therefore you get NULL values for valid dates and 1970-01-01 for invalid dates since those were presumably treated as 0 and 0 seconds since 1970-01-01 00:00:00 is, surprise, 1970-01-01 00:00:00.

Related

Subtract 5 hours from a datetime value that is type timestamp with timezone

I am querying a table that has a datetime column and the value is in the format time stamp with time zone. I've tried doing a select hour(timestamp,-5) as NTime and different variants of that, but the furthest I get is an error stating the timestamp is not a valid name/type. I'm officially going off the deep end on this....
Basically, I just need the new alias column values to be a time stamp that is 5 hours behind the timestamp that is in the original table. Thank you all in advance!!
MariaDB / MySQL doesn't have a "timestamp with timezone" data type.
DATETIMEs are simple wall time, and TIMESTAMPs are UNIX time_t timestamps (the number of seconds since 1970-01-01T00:00:00UTC).
You can convert DATETIME values from one time zone to another by with tz_convert().
SELECT tz_convert('2022-04-08 21:53', 'America/Chicago', 'UTC')
for example.
Or, just to do date arithmetic you can do stuff like this.
SELECT '2022-04-08 21:53' - INTERVAL 5 HOUR

Cannot scan the right values of Order date in sql

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;

Selecting between two timestamps returns no data

I have two unix timestamps 65 seconds apart and i am trying to query mysql in this manner
This is 65 seconds apart 1504684252 + 65
SELECT ask FROM live_rates WHERE the_time BETWEEN
UNIX_TIMESTAMP(1504684252) AND UNIX_TIMESTAMP(1504684317)
SELECT ask FROM live_rates WHERE the_time BETWEEN
FROM_UNIXTIME(1504684252) AND FROM_UNIXTIME(1504684317)
In my table, there is an event starting at timestamp 1504684252 and ending 65 seconds later. Why is there no data returned by either of the queries?.
Your question doesn't really make it clear what data type the_time is - if it's a unix timestamp (i.e. an integer) already, just query it as an integer, given that you seem to know the unix timestamps of your date range:
SELECT ask FROM live_rates WHERE the_time BETWEEN 1504684252 AND 1504684317
If your data in the_time is stored as a datetime, I'd say you should make sure that the server really is converting the values you've supplied to a datetime range that includes the relevant record:
SELECT ask FROM live_rates WHERE id = abcdef
SELECT FROM_UNIXTIME(1504684252), FROM_UNIXTIME(1504684317)
Run these two queries (with the id replaced) and eyeball the data. You'll find the reason, I'm sure - possibly something like a timezone issue, and your unix timestamps are translating to some time in UTC whereas your table data is showing some other time and you've hence mis-translated the time shown into a unix time.. Or possibly that it's an end date that has some time component outside the range of the unix times you specified even though the date part of it is correct.
It's hard to say for sure without a complete, verifiable example (a create table statement with inserted test data and a select demonstrating the problem. Try sqlfiddle.com)
ps; Don't use UNIX_TIMESTAMP() in the way you wrote here; it's not intended to have a unix timestamp passed into it. You either pass it no arguments (in which case it gives you the current datetime of the server clock, as a unix timestamp) or you pass it some datetime (and it will return you the equivalent unix timestamp of that datetime)
If the_time is a unix timestamp as opposed to a date i.e. 1504684252 instead of 2017-09-01 00:00:00, then you don't need FROM_UNIXTIME, which converts a unix timestamp to a date:
SELECT ask FROM live_rates WHERE the_time BETWEEN 1504684252 AND 1504684317
UNIX_TIMESTAMP takes a date/time as parameter and returns an integer. When you're feeding it with an integer it returns 0 - so you're selecting between 0 and 0.
FROM_UNIXTIME takes an integer but it returns a date/time - so if the_time is an integer they're not compatible.

How Insert DateTime Vales into Mysql Databse …Only End User gives Date

I am new developer in .net,I Have requirement..like this ,when user pick date from date picker not time only date he/she pick up,then click insert that time ,i want insert that date and time is into Column exist with name "EnterdDate" data type is "DATETIME". by default 00:00:00 is stored in the Time format I don't want to be stroed that values I want store The at the Time insertion MySql Server Time.
ex:user 12/03/2013 ->insert->click presently assume server time is 13:00:00
i want insert This Date value --> 2013-03-12 13:00:00 ok for me.
after 10 min I want insert the another record at time i want 2013-03-12 13:10:00
like server time in place of default time i am needed
*i don't need the DateAndTime like is :2013-03-12 00:00:00 not Ok for me.
please give best answer the above question.**
Use ADDTIME() to add the CURTIME() to the given date literal:
INSERT INTO my_table
(EnteredDate)
VALUES
(ADDTIME(CAST('2013-03-12' AS DATETIME), CURTIME())
One needs to CAST() the literal to a DATETIME value because ADDTIME() does not work with DATE types.

Convert Date to UTC timestamp in mysql

I have a table with the following columns:
|start_date |TZ |
|Dec 2, 2012 |Eastern |
|Dec 2, 2012 |GMT |
Note 1: our server is in UTC time.
Note 2:The column start_date is a date field, not a timestamp field. Dec 2nd 2012 implicitly means "2012-12-02 00:00:00"
Note 3: The above table is actually multiple normalized tables, but for simplicity, I de-normalized it.
Note 4: I can put anything into the TZ table to make this easy.
I would like to select from my_table where start_date <= now()
However, this doesn't work because of timezone. If the current date/time is
Dec 1st Eastern at 9PM (which is Dec 2nd 1AM UTC), the above query will return both results,
but I really only want the 2nd one. This is further complicated by daylight savings.
Ideally, I would like a query that does the following:
select * from my_table where convert_to_utc_timestamp(start_date,tz) <= now()
The above method would convert start_date to a timestamp and then convert it to the right timezone.
How would I do this in SQL?
There are two functions you'll probably find useful.
The first is:
STR_TO_DATE(start_date,'%M %d,%Y')
That will get your string, in the specified format, converted to a MySQL DATE datatype.
If you have the mysql.time_zone_name et al. tables populated, you can use the function:
CONVERT_TZ()
(need to check that CONVERT_TZ takes a DATE and will return a DATETIME or TIMESTAMP, or include a time component in the string being converted to get a DATETIME, e.g.
STR_TO_DATE( CONCAT(start_date,' 00:00:00'),'%M %d,%Y %T')
Wrap that expression in the CONVERT_TZ() function, e.g.
CONVERT_TZ( datetime_expr ,'US/Eastern','GMT')
To make use of the values stored in your TZ column, those are going to need to match, or you need to come up with a way to match to, the values stored in the mysql.time_zone_name table.