I need to separately insert a Date into one field m_DATE (11-10-2010) and then a time into m_TIME (01:15:03) either 24hr or 12hr with an am/pm. How would I go about doing this.
Sorry I don't have any example to work with I know there is Current_Timestamp but that does everything in one field.
Use
select DATE(CURRENT_TIMESTAMP)
for the date part and
select TIME(CURRENT_TIMESTAMP)
for the time part. If you want the date and the time of another timestamp than CURRENT_TIMESTAMP, you pass your own parameter to the above mentioned function.
Related
I have the datetime format in my Mysql table such as this, on my_date column.
0000-00-00 00:00:00
which will be populated with year, month, day, and time, from [SELECT]'s,
with the onBlur Javascript function.
I need to update, at any time, only one value of the datetime above.
So when I will trigger the year SELECT, it will change the year in the database. When I trigger the month, it will modify the month.
I've searched around and could not find any relevant answer. Thanks!
I have used the first suggestion in the comments, which is to set the full date in my selects (year, month, day, and time), and then use a simple [BUTTON] with onClick -> call to an AJAX that would save the whole date once.
Thanks all!
You can use trick with date_format function for change part of datetime filed like:
-- change year
update tbl set d = date_format(d, '2020-%m-%d %H:%i:%S');
-- change month
update tbl set d = date_format(d, '%Y-03-%d %H:%i:%S');
Test MySQL date_time online
I am stuck for couple of Days on SQL specific scenario. The scenario is as follows,
I have a table, lets call it traffic which has 2 columns -> date and `vehicle (well many more but those are the two I need to match).
The date column is stored as Unix Timestamp. Now this would have been easy to just compare the current date (obtain from php from time() function) however the trick here is that some of these dates have time attached to them also.
For example if you run strtotime(13-02-2017 13:00) and strtotime(13-02-2017) you will get 2 different results. Basically I only care to match the date and not the time.
So I need some way to select the vehicle and date from the database that are equalled to the current Unix Timestamp but with the trick explained above, so I just need to much the date ONLY if possible.
You can use FROM_UNIXTIME() to convert a timestamp to a datetime, and then use the DATE() function to get the date part of that.
WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
However, this can't use an index, so another way that can make use of an index is to check if it's in a range of timestamps for the current date:
WHERE date BETWEEN UNIX_TIMESTAMP(CURDATE()) AND UNIX_TIMESTAMP(CURDATE()) + 86399
(there are 86400 seconds in a day).
SELECT * FROM traffic WHERE DATE(date) = DATE(CURRENT_TIMESTAMP);
I have a cronjob which runs every hour. This cronjob should write to database when dataset was updated. My SQL Query in php looks like this
INSERT INTO allstats (site_id, adformat_id, imps, clicks, conv, net_pub_comp, created_at, updated_at) VALUES (?,?,?,?,?,?,"'.date("Y-m-d H:i:s").'", "'.date("Y-m-d H:i:s").'")
I print out date("Y-m-d H:i:s") and i get 2013-06-28 04:05:17
that is fully right date and time. after that i make a select now() in mysql and time is also the same. timezone on server is set to Europe/Berlin. That timezone is also set in php and mysql. everywhere i get the right time.
But why on insert the datetime column have a value which is 2 hours before the real time? I hope someone have an idea. Thanks for you help.
If the datetime that you're inserting is the datetime of script execution why not just use NOW() in your insert statement?
INSERT INTO allstats (site_id, adformat_id, imps, clicks, conv, net_pub_comp, created_at, updated_at)
VALUES (?,?,?,?,?,?,NOW(),NOW())
Less parameters to bind and pass.
MySQL date/times should be stored in the UTC time zone. Your application should calculate the difference and convert that date/time into the appropriate time zone.
I use MySQL DATETIME column to store date & time. Dates are in UTC. I want to select item from one day. What i'm doing now:
SELECT * FROM data WHERE DATE(CONVERT_TZ(datetime, 'UTC', 'Australia/Sydney')) = '2012-06-01'
note that the timezone depends on user
Problem is that it is quite slow with table growing.
Is there any solution how to make it faster?
Currently your query has to compute the conversion for every row of the database. You probably could make things better by converting the other way round, so that the conversion only occurs once (or actually twice, as you'll have to form a range). Then a proper index on datetime should make things pretty fast.
SELECT * FROM data
WHERE datetime BETWEEN CONVERT_TZ('2012-06-01 00:00:00', 'Australia/Sydney', 'UTC')
AND CONVERT_TZ('2012-06-01 23:59:59', 'Australia/Sydney', 'UTC')
Or if you worry about a 23:60:00 leap second not getting matched by any query, you can do
SELECT * FROM data
WHERE datetime >= CONVERT_TZ('2012-06-01', 'Australia/Sydney', 'UTC')
AND datetime < CONVERT_TZ('2012-06-01' + INTERVAL 1 DAY, 'Australia/Sydney', 'UTC')
In the latter form, you wouldn't have to add the hours PHP-side but instead could simply pass the date as a string parameter.
Depending on your real goal, using TIMESTAMP instead of DATETIME may be a good solution.
TIMESTAMP stores the datetime as UTC, converting as it stores and as it is fetched, from/to the local timezone. This way, what I read from your table is automatically different than what you stored (assuming we are in different timezones).
Yes, use #MvG's approach of flipping the query. Yes, use his second form.
And index the column. In composite indexes, put the timestamp last, even though it is more selective.
DO NOT do SELECT *
Indexing - make sure apropriate colunms/id
fields are indexed.
Do time-conversion php-side.
OR make sure you do 1 & 2 and it may be wrapped into a Stored Proc, passing timezone as param.
Currently MySQL query will be as below:
SELECT * FROM data
WHERE datetime >= CONVERT_TZ('2012-06-01', '+00:00', '+10:00')
AND datetime < CONVERT_TZ('2012-06-01' + INTERVAL 1 DAY, '+10:00', '+00:00')
I want to use PHP to insert date into mysql datetime field.
$time = date('Y-m-d');
mysql_query(INSERT INTO A (date) VALUE ('$time'));
Table A date column is datetime field. I want it shows 2011-01-24. But it always shows 2011-01-24 00:00:00. So my problem is how to remove the suffix "00:00:00".
Since the field is a DATETIME field, it stores the DATE and the TIME. If you want to exclusively store the date, make it a DATE field.
Change the column type to DATE instead of datetime!
A bit unclear, I see 2 possible solutions:
If you're referring to removing the suffix 00:00:00 from the MySQL, you need to change the field type from datetime into a date.
If you're referring to the removal of the suffix from the php script, well you can always use
mysql_query(SELECT DATE_FORMAT(date, '%d-%m-%Y') FROM a);
read up mysql date and time functions
Change the field to a DATE field in your table. Your PHP code is fine as it is.