In my MYSQL Database. I have a 'Date' and 'Time' type. When I input data into the SQL table. I'd like the time to show what time the data was submitted. I've looked around and found nothing.
If someone could tell me how I can fix this much appreciated.
Thanks in advance.
You can alter your db table schema, here is how you can change by phpmyadmin
Or by query you can do like this
ALTER TABLE `tablename` CHANGE `date_created` `date_created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
Okey, after a complete discussion. you want to insert a datetime in a timestamp filed, but want ot show the date and time different in your PHP code.
The date function convert a microtime to any custom format of date, time.
and the strtotime convert the time to mictotime.
Try this:
In PHP just use
$Datetimestamp = //fetch the timestamp field
This is for Date: date("Y-m-d" , strtotime($Datetimestamp))
and this is for time: date("H:i:s" , strtotime($Datetimestamp))
Related
I'am using a script that saves some fields into my database as Epoch & Unix Timestamp format, (I think because the field is submitting as something like 1515469971), the column is an "int" format type.
I want to know, how I can make a query, to search all the inputs that have created on the current month.
I am so confused about it, hope you can help me :)
Firstly, show me your table structure with show create table table_name.
If you're using an int to store the time. There are two ways to do that:
Convert the field to a date string with from_unixtime. Then you got a string like '2018-01-08 05:05:05', and just compare the month value.
Calculate the very beginning and the end of the current month in advance. Then just select * from table_name where time >= beginning_of_the_month and time <= end_of_the_month.
I have what is most likely a very simple question.. I am designing a simple blogging system and I am trying to put the current date into the table where the blog post is stored whilst waiting for administrator approval. but the method I have used puts 0000-00-00 into the date column! What I am using is as follows:
$query = "INSERT INTO blogentry VALUES ('".$mnam."','".date('d-m-Y h:m:s') ."\n"."','".$mcom."','".$approve."')";
I am relatively new to php so stumble accross errors like this all the time... but I cant seem to google this one!
Thanks guys!
So the easiest way to do this is just let MySQL handle it with the NOW() function:
INSERT INTO blogentry VALUES( ..., NOW(), ... )
Another option is to use TIMESTAMPs by changing your table - set the column to type TIMESTAMP with DEFAULT CURRENT_TIMESTAMP, and you can just ignore that column when inserting - it will automatically be filled with the current time. You will need to specify the columns you're inserting to in order to skip a column:
INSERT INTO blogentry( column1, column2 ) VALUES( column1value, column2value )
Finally, you NEED to sanitize your inputs. Preferably using prepared statements and PDO (http://php.net/manual/en/pdo.prepared-statements.php), or at least using mysql_real_escape_string.
From the MySQL manual on DATE, DATETIME
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
This means you have to insert the dates in YYYY-MM-DD format. You are using date('d-m-Y h:m:s') format. Change that to date('Y-m-d') and it should insert correctly.
If you want the time as well, then you need to change the column datatype to DATETIME and then insert using the format date('Y-m-d H:i:s').
As other mention, you can use an INT column type instead and store a Unix timestamp which is stored in UTC so it is more portable. You can then easily manipulate the timestamp to output the date any way you would like.
Try just storing a strtotime() result. It creates a unique timestamp, which can then be parsed however you need it in the future.
You might need to give the timestamp to the date function:
date('d-m-Y h:m:s', strtotime('now'))
Also, to do a standard datetime format:
date('Y-m-d H:i:s', strtotime('now'))
I am trying to set the value of a field via a hidden form field to the current date and time using either PHP or Javascript that would conform to MySQL's datetime field.
You can use PHP to get and format the current system date/time for use in MySQL like this:
$now = date('Y-m-d H-i-s');
You can directly set current date and time in your SQL insert query using NOW():
INSERT INTO table_name (current_time, column2, column3,...)
VALUES (NOW(), value2, value3,...)
where current_time is the field where you want to put current date and time.
Create the column using DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP
Those together will make it so that any new rows inserted have the current time and are updated again when the column is updated.
Example:
CREATE TABLE test (last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)
Edit: Nevermind, this will use a TIMESTAMP column, not DATETIME. Other answers will do what you want.
<?php echo time(); ?>
will output a nice simple integer number that you can pass directly into MySQL and convert into a native mysql datetime value with FROM_UNIXTIME(). It'll save you the trouble of formatting the data in a nice YYYY-MM-DD hh:mm:ss string.
$current = date_timestamp_set(date_create(), time());
I'm working in PHP with a MySQL db and I have a current timestamp field which is created when the field is made, I then have another field which (when a page is hit) I would like a SQL statement to insert a replica of the timestamp - only 2 days ahead. Any ideas on how I would go about doing this?
So you have a table like:
id
current TIMESTAMP DEFAULT CURRENT..
another TIMESTAMP
?
You can do something like
UPDATE MyTable SET another = ADDDATE(current, INTERVAL 2 DAY) WHERE id = :myId
MySQL Date and Time functions
How about INSERT [...] (... , ADDTIME(NOW(),'2 00:00:00' , ...)
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.