Update MySQL Date field with single INT from another field? - php

For simplicity sake, I have two fields within a table:
Date 1 (YYYY-MM-DD format)
Day (single or two digit day format, 1-31)
I want to be able to update Date 1 using the value within Day but I DO NOT want to make multiple calls to do so (first a select, fetch results, then update with the result from the same table).
ultimately, the 'design' of my call (which does not work) would be:
UPDATE table SET Date 1 =
DATE(Y-(M+1)-(value of Day));
or in php:
date("Y-m-d", mktime(0,0,0,date('m')+1, VALUE(Day), date('Y')));
is this possible?
UPDATE
==
While I have been able to utilize some of the code below, I am not sure MYSQL is 'smart' enough to run the calculation as I have it. My new code is:
UPDATE table SET Date 1=
CONCAT(YEAR(CURDATE()),'-',MONTH(ADDDATE(CURDATE(),
INTERVAL 1 MONTH)),'-',Day1)
While this returns the correct 'new month' and 'new day', the year will be wrong WHEN the current month is December.
For example: If the current date is 2010-12-02. The preferred data in the Day field is 12. Once our script has processed, the Date 1 field should be updated to 2011-01-12 but in the code above it will only output to 2010-01-12.

not tested, but i think what you're missing is CONCAT:
UPDATE table SET datefield = CONCAT(YEAR(datefield),'-',MONTH(datefield),'-',dayfield);
after rereading you questioon, it sounds like you want to add the days, that would be like this (not tested, too - take a look at DATE_ADD and INTERVAL):
UPDATE table SET datefield = DAT_ADD(datefield, INTERVAL dayfield DAYS);

Related

How to change specific element in MYSQL datetime?

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

how to automatically update current date to 1 week in php to the server

I already have SQL table with date column mentioned below.
SL.no Date name
1 15-02-2017 krish
I want to automatically update column date to one week when the current date is equal to table column date.
Example
Today date is 15-02-2017
My SQL column date is 15-02-2017
Then i want to update this automatically to 1 week when i access my home page.
Is this possible to do?
I hope you understand my questions.
Thanks in advance.
Looking to your example data your date field is a VARCHAR or CHAR type.
You need to use STR_TO_DATE to make it possible to use + INTERVAL 1 WEEK and DATE_FORMAT to convert back to your CHAR or VARCHAR field.
UPDATE
[table]
SET Date = DATE_FORMAT(STR_TO_DATE('15-02-2017', '%d-%m-%Y') + INTERVAL 1 WEEK, '%d-%m-%Y')
WHERE
Date = '15-02-2017'

changing date fields with incorrect year using sql in phpMyAdmin

I have a live db that has some 2011 dates that were entered as only mm/dd and these defaulted to 2012 dates. (eg. 2011-03-16 was entered as 03-16 and stored as 2012-03-16)
As a result, the db has some transaction dates in the future. I have fixed the code to correct the input, but I need to fix the incorrect dates in the db.
I can manually edit these with phpMyAdmin but that would take hours.
The ideal solution would be SQL to correct these dates in the SQL window in phpMyAdmin.
The selection would be:
SELECT created FROM posts WHERE created>'2012-02-01'
and then subtract 1 year from each result and store it.
To phrase it another way, I need to take each created field and if it is a future date subtract 1 year to make it a 2011 date, and then save it.
UPDATE posts SET created = DATE_ADD(created, INTERVAL -1 YEAR)
WHERE created > CURDATE()
Should be pretty simple using the DATE_ADD() function.
update posts set created = DATE_ADD(created, INTERVAL -1 YEAR) where created > '2012-02-01'
source: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
edit: oops, updated syntax.
UPDATE testdate
SET DATUM = DATE_ADD(datum, INTERVAL -1 YEAR)
WHERE DATUM > NOW();

Add 10 minutes into the future into a MySQL db using PHP/PDO

I want to add a row to a db with a time 'x' amount into the future (different for each row). I have looked through every tutorial/help/whatever I could find via google but nothing seems to help.
From what I can tell, the best way is to do it with MySQL.
SELECT ADDTIME(now(), '00:10:00')
But how do I actually make that work with this:
$new_row = $db->prepare("INSERT INTO table (field1, field2, time) VALUES(?, ?,?)");
$new_row->execute(array($field1, $field2, $timeintenminutes)) or dieWithDBError($new_row);
Untested, but have you tried:
$new_row = $db->prepare("INSERT INTO table (field1, field2, time) VALUES(?, ?, ADDTIME(NOW(), '00:10:00'))");
$new_row->execute(array($field1, $field2)) or dieWithDBError($new_row);
What is the column type of time?
In PHP, you can simple do that $AddSomeTime=time() + 60; which will add 1 minute from now.
I presume want that instead of updating the MySQL table? If you want to work with MySQL update query, than use UPDATE YourTable SET valuet=(ADDTIME(now(), '00:10:00')) WHERE id=$id;
$future_time = date("Y-m-d H:i:s",time()+60*10);
This will get you the 10 minute added time.
In Php strtotime() will do the trick for adding values to time or date
$1hrInFuture = strtotime('now +1 hours');
str2time (now); will understand now as the server's current time, and will add 1 hrs to it, you can add +1 weeks, +1 months...etc.
to make it formatted correctly, use the date:
$1hrInFutureF = date('Y-m-d H:i:s',$1hrInFuture);
this example will get the current servers date, and add 1hr to it;
Note: not using 'now' in the strtotime will generate the date 'January 1 1970 00:00:00 UTC';
Now you can get the date from the database, insert it into the strtotime, add 1 hr to it, format it the way you want, and insert it back to a new column.
and for the different timezone between the mysql n the server, you can create a column in that db, set a randdom hash inside the inserted row, the select that row's normal_date, and insert the +1 hr date using the current_time_stamp and the random number
INSERT INTO X (title,rnd_nb) VALUES ('titke','rnd_nb);
SELECT ID FROM X WHERE rnd_nb = $rnd_nb(php variable inside the function);
UPDATE `x` SET (1hrplus_date='$the_variable_containing_the_future_time') WHERE id='the_id_grabbed_from_the_second_query);
So if you see, we inserted a random number there,automaticaly if its the currenttimestamp, it will add the current_time_stamp..etc and selected the row containing that number, got it's id, , then get the currenttimestamp, put it in the strtotime function to add 1 hr to it, update that row, and set the +1hrcolumn using the strtotime output.
Hope that's useful :)

How to sort data depending on what day it was posted?

How do you sort data which was stored in a mysql database depending on the days of the week in which the data was submited ??
I basically want to create a diary which outputs information in each day of the week depending on what day it was posted by dates so,
Mon - Data in order of date
Tue -
Wed - e.t.c
Any code examples and information will be great, thanks.
You can do a
SELECT DAYOFWEEK(datehere) as dayofweek, datehere FROM something ORDER BY dayofweek, datehere;
You can use the DAYOFWEEK function to extract the day, and then sort on it just like any other data.
What kinf of data type is the column where you store the date submission?
It seems like you're asking for a basic SELECT statement?
SELECT some_column, another_colum FROM your_table ORDER BY your_date_column DESC
This assumes you actually have a column that logs the insertion timestamp.
If this answer is obnoxiously simplistic, please forgive me...and give us more details :)
Regards.
If your data is stored as a DATE or DATETIME field, use the DAYOFWEEK or DATE_FORMAT functions to turn it into day name for output, but continue to order by the DATE field
SELECT DATE_FORMAT(my_date_column, '%W') AS dayofweek
FROM my_table
ORDER BY my_date_column
Well, the sorting bit is easy, just sort on the column that represents the post's date. The grouping in days is something you can do in your code, since you need to check the date there anyway (for post-processing the actual output).
To put it this way, you can do a subselect to get the specific day of the week, but in your code you would have to check the day again to group posts per day. In that case it's better (and cleaner, since you're separating business logic from data) to do this in your code, something like this:
select all posts (within date range)
make an associative array, with the
days as keys, and posts (in new
arrays) as values
loop through the
days and then posts to output the
posts per day
SELECT *
FROM diary_entries
ORDER BY FIELD(DAYOFWEEK(created), '2, 3, 4, 5, 6, 7, 1'), created
DAYOFWEEK grabs day of the week number (1 = Sunday).
FIELD makes Monday first day of the week.
After sorting by day of week, then sorted by date created.

Categories