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.
Related
How do I insert the current date to my database? I have a column called date to put it on.
I want to insert it at the same time I insert this:
$sql="INSERT INTO `Lines` (Text, PID, Position)
VALUES
('$text','$pid','$position')";
Is there a way to automate it in PHPMyAdmin or it's the same to do it this way? Thanks
If the table definition has the timestamp column default set to CURRENT_TIMESTAMP, you actually don't have to do anything at all. Otherwise, NOW() and CURRENT_TIMESTAMP will work, as in:
INSERT INTO t1 (timestamp_column) VALUES (NOW());
There is a difference between CURRENT_TIMESTAMP and NOW() but it's probably too small to matter to you.
phpMyAdmin seems like it has CURRENT_TIMESTAMP as an option when creating a new column.
INSERT INTO `Lines` (`date`) VALUES (NOW());
Depending on your requirement, you can also do this
$date=date('d.m.y h:i:s');
And then insert $date. I mean if you only want to view the date & time. Otherwise i also recommend time().
Otherwise... if you're using an integer, as is common in PHP, just use time() and insert it's value the same way you inserted the other variables, or use MySQL's UNIX_TIMESTAMP()
The best way to store timestamps is as UNIX timestamp integers in GMT / UTC. This allows you to always know the exact time no matter where you, your server, or your users are located. A bonus is that you can allow your users to set their timezone and display times meaningful to them.
$sql = "INSERT INTO `Lines` (`timestamp`) VALUES ('" . time() . "')";
or
$sql = "INSERT INTO `Lines` (`timestamp`) VALUES ( UNIX_TIMESTAMP() )";
Be careful if you choose to use NOW() or CURRENT_TIMESTAMP as they are managed by the database server and it's settings. If the server your site is hosted on is in one time zone and you move to another host in another timezone all of your timestamps will be incorrect. Using Unix integers will add a little extra effort wherever you application deals with times but it gives you the most accuracy and the most flexibility.
I am trying to construct a mysql query string to pull out certain records but only if the date in the database is greater than the current date.
So I have this so far and I am not sure if this is a legal syntax...
date_default_timezone_set('America/Los_Angeles');
$current_date = date("Y-m-d");
$sql = "SELECT * FROM `coupons` WHERE status = 1 AND end_date > '$current_date'";
Thanks for your help.
It's legal syntax. You can use one.
I use CURRENT_TIMESTAMP in general, i.e.:
SELECT * FROM `coupons` WHERE status = 1 AND end_date > CURRENT_TIMESTAMP
Do you have a need to compare to the LA timezone? CURRENT_TIMESTAMP will use the local MySQL server time (but that should technically be what the date values are stored as, as well).
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());
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)
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()