MySQL - Update date/time - 10 mins. in future - mysql

Is it possible to update date/time in MySQL directly.
UPDATE tasks SET date_due='2014-12-01 10:30:00' WHERE tasks.id = '97534f55-32a9-8ef3-2e2f-547c3782d5e6' AND deleted=0;
I need to update time, 10 mins. in future.
The said time is not current time therefore I cannot use DATE_ADD(NOW()
Thanx

You can use date_add() on a datetime value not just now()
mysql> select date_add('2014-12-01 10:30:00',interval 10 minute) as date ;
+---------------------+
| date |
+---------------------+
| 2014-12-01 10:40:00 |
+---------------------+
1 row in set (0.00 sec)
So it will be
UPDATE
tasks
SET date_due=date_add(date_due,interval 10 minute)
WHERE tasks.id = '97534f55-32a9-8ef3-2e2f-547c3782d5e6'
AND deleted=0;

Related

Convert datetime column to 24 hour format

I have a column called schedule_time (datetime) format. I want to convert the time to 24 hour time.
2016-03-08 03:00:00 to 2016-03-08 15:00:00
Please note that DATETIME values are always stored in 24h format (http://dev.mysql.com/doc/refman/5.7/en/datetime.html). There is no AM/PM.
When you want to display the values, there is however the DATE_FORMAT
function, which will format the value according to your needs, including AM/PM:
select DATE_FORMAT(schedule_time, '%Y-%m-%d %h:%i:%s %p') from t1;
This will give 2016-03-08 03:00:00 AM and 2016-03-08 03:00:00 PM. But the values in the DB are still the same, in 24h format.
If adding 12 hours would solve your issue, the you can do it like this:
start transaction;
update t1 set schedule_time = date_add(schedule_time, interval 12 hour);
select * from t1; -- verify!!!
rollback;
-- or commit;
I put this in a transaction so you can first verify your results. If they are wrong, simply rollback the transaction (provided you use InnoDB tables). If you don't have transactions (or feel uncomfortable with them), you can undo the change with date_sub instead of date_add.
But be aware: This doesn't change from 12h to 24h format, it simply adds 12 hours to all your schedule_time values.
Use MySQL's DATE_FORMAT function.
The format string will be '%Y-%m-%d %T'.
Selecting the current date with 24-hour time:
mysql> SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %T') AS now
+---------------------+
| now |
+---------------------+
| 2016-03-08 20:47:04 |
+---------------------+
1 row in set (0.00 sec)
Selecting a date with 24-hour time from a table:
mysql> SELECT DATE_FORMAT(`created_at`, '%Y-%m-%d %T') AS created_at FROM test.comments;
+---------------------+
| created_at |
+---------------------+
| 2016-02-25 16:32:12 |
+---------------------+
1 row in set (0.00 sec)

NOW Function is not working in my query

My NOW Function is not working the way I want it to. I just want to simply find dates that are past todays date.
Here is my query.
SELECT *
FROM `trade_show_inventory`
LEFT JOIN `trade_show_reserved`
ON `trade_show_inventory`.`id` = `trade_show_reserved`.`productid`
WHERE `trade_show_inventory`.`quantity` > 0
OR `trade_show_reserved`.`datereserved`
+ INTERVAL 5 day <= '2013-03-31'
AND `trade_show_reserved`.`datereserved` > Now()
EDIT:
I changed my query to this and it still is not working. Still working away at it..
$date = date('Y-m-d');
$sql = "SELECT * FROM `trade_show_inventory` LEFT JOIN `trade_show_reserved`
ON `trade_show_inventory`.`ID` = `trade_show_reserved`.`ProductID`
WHERE (`trade_show_inventory`.`Quantity` > 0)
or (`trade_show_reserved`.`DateReserved` + INTERVAL 5 DAY <= '$setupStart' and
`trade_show_reserved`.`DateReserved` > '2013-03-25')";
Well, your where clause is parsing like:
WHERE (`trade_show_inventory`.`Quantity` > 0) or
(`trade_show_reserved`.`DateReserved` + INTERVAL 5 DAY <= '2013-03-25' and
`trade_show_reserved`.`DateReserved` > NOW()
)
If the date is five days before 2013-03-25, then it can't be in the future (as of today at least).
Put parentheses around the where clauses to get the logic you intend.
NOW() doesn't return a date... it's a complete timestamp:
mysql> select NOW();
+---------------------+
| NOW() |
+---------------------+
| 2013-03-25 17:20:06 |
+---------------------+
1 row in set (0.00 sec)
Try wrapping the DATE function around it:
mysql> select DATE(NOW());
+-------------+
| DATE(NOW()) |
+-------------+
| 2013-03-25 |
+-------------+
1 row in set (0.00 sec)
NOW() returns the current date & time.
You would perhaps want to use DATE() as it returns the date without time.
The NOW() function uses timestamp, i think you should first convert it into a date format that you require.

How to convert UNIX time before 1970 to date format in MySQL?

I have a database using unix time for its dates ( i am using mySQL). I want to retrieve the dates in daily date format. This is my query:
SELECT FROM_UNIXTIME(time_created) FROM member
This works fine with dates after 1970 (for example, 1314162229) but doesn't work for dates before 1970 (for example, -769338000). Is there any work around here?
A possible workaround would be to have a constant handy corresponding to the seconds in a certain number of years (preferrably a multiple of 4). You could add this constant, translate the time and then subtract the number of years chosen.
Example: choose 40 years.
Determine the constant:
MySQL [files]> select adddate(from_unixtime(0), interval 40 year);
+---------------------------------------------+
| adddate(from_unixtime(0), interval 40 year) |
+---------------------------------------------+
| 2010-01-01 01:00:00 |
+---------------------------------------------+
1 row in set (0.09 sec)
MySQL [files]> select unix_timestamp(adddate(from_unixtime(0), interval 40 year));
+-------------------------------------------------------------+
| unix_timestamp(adddate(from_unixtime(0), interval 40 year)) |
+-------------------------------------------------------------+
| 1262304000 |
+-------------------------------------------------------------+
1 row in set (0.09 sec)
Now you can every unix timestamp x between 1930 and 20xx and use it.
select subdate(from_unixtime(x+1262304000), interval 40 year);
With your example -769338000, you get
MySQL [files]> select subdate(from_unixtime(-769338000+1262304000), interval 40 year);
+-----------------------------------------------------------------+
| subdate(from_unixtime(-769338000+1262304000), interval 40 year) |
+-----------------------------------------------------------------+
| 1945-08-15 17:00:00 |
+-----------------------------------------------------------------+
1 row in set (0.09 sec)
I found a new way:
converting to MySQL date:
SELECT DATE_ADD(FROM_UNIXTIME(0), interval YOURTIMESTAMPHERE second);
converting your epoch to a date string:
SELECT DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), interval YOURTIMESTAMPHERE second), '%Y-%m-%d');
And back
SELECT TIMESTAMPDIFF(second,FROM_UNIXTIME(0),'1960-01-01 00:00:00' );
source:
http://www.epochconverter.com/programming/mysql-from-unixtime.php#negavtiveEpoch
SELECT DATE_ADD(CAST('1970-01-01 00:00:00' AS DATETIME), INTERVAL `time_created` SECOND) FROM `member`
To my knowledge there is no such thing as UNIX time prior to 1/1/1970 00:00 UTC. More at Wikipedia.

Date time comparisons

I have a date column for ex: 03/08/2011 & i need to make that as end of the day ie. 03/08/2011 12:00:00 in UTC time format & then add some integer value in minutes say for ex:10 (in mins)-> 03/08/2011 12:10:00. This value is used as end value.
There will be some start value like for ex: 03/07/2011 22:10:00 & i need to get current date time in UTC, do a comparison to see & return 1 if current date time falls between start & end value else return 0.
All in a single select query in mysql.
Thanks.
It's not clear exactly what you need, but I'll give you a couple of specific examples that should help.
1) Converting a MM/DD/YYYY string to a date and adding 10 minutes to it:
mysql> SELECT STR_TO_DATE('03/08/2011', '%m/%d/%Y') + INTERVAL 10 MINUTE;
+------------------------------------------------------------+
| STR_TO_DATE('03/08/2011', '%m/%d/%Y') + INTERVAL 10 MINUTE |
+------------------------------------------------------------+
| 2011-03-08 00:10:00 |
+------------------------------------------------------------+
1 row in set (0.00 sec)
2) Getting the current timestamp in UTC:
mysql> SELECT UTC_TIMESTAMP();
+---------------------+
| UTC_TIMESTAMP() |
+---------------------+
| 2011-03-08 20:33:53 |
+---------------------+
1 row in set (0.00 sec)

Filter by current date in MySQL

How to pass the current date to a query in mysql like such query:
select from Dailytimesheet dailytimesheet where dailytimesheet.TrackingDate="2010-05-03"
In MySQL, you can use CURRENT_DATE to get the current date.
mysql> select CURRENT_DATE;
+--------------+
| CURRENT_DATE |
+--------------+
| 2010-05-03 |
+--------------+
1 row in set (0.08 sec)
Using NOW() works as well, but gets you the current date and time as a timestamp value. You can truncate it like DATE(NOW()), but CURRENT_DATE avoids the function call.
you can use the NOW() function within your SQL query to get the current timestamp.