Set current time field for MySQL - php

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

Related

PHP - Putting a date into a MySQL table

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

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.

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)

How to put the current datetime in MySQL using PHP

How to put the current datetime in MySQL using PHP?
The result should be:
mysql_query("INSERT INTO users (r_date) VALUES ('the current datetime')");
The type of r_date is datetime.
If your field is a DATE or DATETIME, NOW() will work:
INSERT INTO users (r_date) VALUES (NOW());");
Note that the now() function insert current date of MySQL server.
If you need to insert server date you should use php date function

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