How to change specific element in MYSQL datetime? - php

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

Related

How to display some content only for some time or some days

I want to display some content only for some time or some days or repitative
the start time , start date , end date and end date will be stored in database
so when the current time is in between start date, start time, end date and end time the content should display
How to achieve this... Please Help Me
Thanks in advance
There aren't a lot of details in this question to go on, but let's say you're using the MySQL database, and you have a table like you've described:
mytable:
start_time TIME
start_date DATE
end_date DATE
content TEXT
So, you could make use of MySQL's date functions to help you select the appropriate content, and possibly use PHP's date as well, as suggested in the comments.
For example, let's say you wanted to construct a query that retrieved content for "today".
You could use something like:
SELECT content FROM mytable
WHERE CURRENT_DATE BETWEEN start_date AND end_date
AND start_time <= CURRENT_TIME
Have a look at the PHP date function so that you can construct a parameterized query with alternate values.

php date() mysql datetime

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.

mysql query, how to go about it?

I am storing data in a mysql database with unix time()
I want to select data out for days, and echo it out like this:
Monday - value
Tuesday - value
Wednesday - value
Thursday - value
Friday - value
Saturday - value
Sunday - value
value is saved in the database as value
the times are saved in the database as time
So basically I want to select time from the database in a query that groups all of them as a day, Monday.. do you get what I mean?
Any help with this would be greatly appreciated.
First, replace that integer with a real DATETIME column, then use MySQL's built-in functions to easily GROUP BY the DAYOFWEEK(column)
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
There is no advantage to storing an integer instead of a real date. Not only can you easily convert to a UNIX timestamp if you need that in your application, but you'd save storage space too.
To convert to MYSQL native date, use FROM_UNIXTIME(). DAYNAME will return the day of the week.
SELECT DAYNAME(FROM_UNIXTIME(col))
For example:
SELECT DAYNAME(FROM_UNIXTIME(1196440219))
Further, to group by you could use something like this:
SELECT DAYNAME(FROM_UNIXTIME(col)), ... FROM table GROUP BY DAYNAME(FROM_UNIXTIME(col))
Maybe hits post can help you: MySQL Query GROUP BY day / month / year
Regards!

Update MySQL Date field with single INT from another field?

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);

mysql date and time auto stamp?

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.

Categories