SQL: Insert DateCreated value? - php

In my table I have a dateCreated column in the format of DATETIME.
How do I insert the current datetime when I add the record to the database?

Better use TIMESTAMP instead and use CURRENT_TIMESTAMP to get current timestamp. TIMESTAMP should behave exaclty (or very similar) to DATETIME so you shouldn't notice a difference.
Furthermore you may set CURRENT_TIMESTAMP as default for all new entries.

Use the CURDATETIME()

Related

Datetimestamp not working

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

How to Manually Adjust Value of timestamp To Previous Year?

I am using timestamp to store the pubdate of uploads. I want to add a filter to sort records by the year they were uploaded so need to change a couple of records to previous years for testing purposes. When I try to manually adjust the date from within mysql by clicking the calendar and picking a date it flags errors.
The value of timestamp is set to CURRENT_TIMESTAMP.
The values of function in the timestamp field are :
NOW
CURDATE
FROM_DAYS
FROM_UNIXTIME
PERIOD_ADD
PERIOD_DIFF
TO_DAYS
I cannot find anything online relating to this. I don't need to perform calculations I need to manually adjust the date.
In case the problem is that you used an ON UPDATE CURRENT_TIMESTAMP on this column you can remove this setting by redefining your column:
ALTER TABLE yourTable MODIFY COLUMN pubdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
You might also check the docs:
With neither DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP,
it is the same as specifying both DEFAULT CURRENT_TIMESTAMP and
ON UPDATE CURRENT_TIMESTAMP.
And I guess your table was created without defining any of those two resulting in the behavior that you cannot update this column as it is updated automatically.
What happens is:
You pick a row and overwrite the timestamp (like setting the date to 2013)
MySQL detects that the row was updated
as ON UPDATE CURRENT TIMESTAMP is implicitly specified MySQL updates the timestamp field with the current timestamp
Your changes to the timestamp will be overwritten / lost

Set current time field for MySQL

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

Add date to mysql from php for sorting later

when inserting a date into a MySQL DB using PHP what is the best format so that I can sort by date later. I started using
$current_time = date("Y-m-d");
Is this the best practice?
If you are able to control your database fields then I would recommend using MySQL's built in Timestamp data type. You can set it to the current time by default.
CREATE TABLE IF NOT EXISTS `your_table` (
`date` timestamp NOT NULL default CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
If not then I would reccomend just storing the default PHP Unix formatted timestamp in an integer field.
$current_time = time();
The database handles dates internally for storing and sorting. Y-m-d format is good
You do not ever need to generate the current date or time in PHP to insert it into a query. Use the MySQL constants CURRENT_DATE and CURRENT_TIMESTAMP in the query instead.
INSERT INTO table (name, date) VALUES ('Bob', CURRENT_DATE)

datetime function in php

I want to store the data and time in my MYSQL db.I have a datetime field in my db
I want to store current datatime in my db
How shold i get the current date time?How to pass it to db via a sql query
How can i then retriev an print it in correct yyyy--dd--mm format or any other format
What wil be format of time? wil it be 23 hrs etc?
How do i print date and time?
You can let MySQL determine the current timestamp by using Now() in your query.
INSERT INTO foo (dtfield) VALUES (Now())
This can also be done with a default value for a TIMESTAMP field in your table definition
CREATE TABLE foo (
id int auto_increment,
creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
v int,
primary key(id)
)
You could use MySQL's built in date/time functions if you only want to insert it into the MySQL database.
Have a look at: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Otherwise, you can use the PHP's date() function.
Assuming table named 'items' and field named 'modified' of type 'timestamp'
$r = mysql_query("INSERT INTO items (modified, x, ...) VALUES (CURRENT_TIMESTAMP, $x, ...)");
// (or "UPDATE items SET modified=CURRENT_TIMESTAMP, x=$x, ...)
...
$r = mysql_query("SELECT UNIX_TIMESTAMP(modified) FROM items");
$item = mysql_fetch_assoc($r);
$formatted_ts = date('g:ia', $item['modified']); // or another format *
you'll need to add appropriate error-checking which I've omitted; also need to adjust for consideration of timezones, which I've also left out
see date()
You might also want to set your timezone.

Categories