I have made the following table on mysql to save the time that a client visit the page:
CREATE TABLE `visitas` (
`**idv**` int(11) AUTO_INCREMENT,
`**fecha**` timestamp **DEFAULT CURRENT_TIMESTAMP**,
`**ip**` varchar(15)
PRIMARY KEY (`**idv**`))
On php I run the next query:
mysql_query("INSERT INTO `visitas`(`ip`) VALUES ('$ip')");
The problem is that mysql save the time of the United States, and I want the time of Mexico.
I have tryed:
mysql_query("SET time_zone = '-06:00'");
but it doesnt take effect.
Please help me to make that mysql automatically save the time that i want.
To set timezones with mysql, use SET GLOBAL time_zone = '-06:00';
or just set it with php
mysql_query("insert into visitas(ip, timestamp) values ('$ip', '". date('Y-m-d H:i:s', time()) ."');
also, dont use mysql, look at mysqli or PDO
Related
I am using PHP and MySQL. I have following insert query which is working fine.
$query="INSERT INTO mytable (user_id, user_datetime) VALUES ".
"(".$_SESSION["user_id"].", NOW());";
Problem is that:
1) When I was testing on my localhost, NOW() was picking date and time from my system. That was good, no problem!!!
2) Now I have hosted my site to a web hosting server which is anywhere in Canada and I am in India. Now if any insert is happening, it is storing the time of Canada not of India. This is the problem. How can I get rid of this. How do I save Indian time in my database?
You have two solutions: use the PHP approach or the MySQL approach. (your question title is misleading, it's not a PHP NOW() but a MySQL NOW()).
With PHP, you can use the date_default_timezone_set function for this.
It will set the default timezone for your PHP script.
Just remember to put it near the top so it applied to what you are doing.
BUT this will set the timezone for all PHP function, not your MySQL database NOW() command.
To modify the timezone of MySQL, you have to use SET time_zone = timezonename;. See this SO thread for setting MySQL timezone or the official documentation.
Example:
<?php
date_default_timezone_set('Asia/Calcutta');
$query = "INSERT INTO mytable (user_id, user_datetime) VALUES ".
"(".$_SESSION["user_id"].", ".date('Y-m-d H:i:s').");";
// OR
$query = "SET time_zone = 'Asia/Calcutta';"; // this will persist only for the current connection
$query .= "INSERT INTO mytable (user_id, user_datetime) VALUES ".
"(".$_SESSION["user_id"].", NOW());";
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.
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)
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.
I'm using PHPMyAdmin and I've got a MySQL table column called "timestamp." The type (surprise!) is TIMESTAMP, and in 'attributes' I've set it to ON UPDATE CURRENT_TIMESTAMP.
However, each new record gets a timestamp that looks like this:
0000-00-00 00:00:00
I have explicitly set the default value to none, but when I save and come back to look, it is set to all zeros as above.
The relevant PHP records page hits with this query:
$query = "INSERT INTO `pagehit` (user_id, pageurl)
VALUES ('" . $userid . "', '" . $pageurl . "')";
The whole thing is running under XAMPP.
What am I missing?
What am I missing?
You don't update :)
Use DEFAULT CURRENT_TIMESTAMP along with ON UPDATE CURRENT_TIMESTAMP
Try setting the default value to CURRENT_TIMESTAMP instead of putting that in the attributes.
MySQL Reference
If your timestamp column captures only the insertion time then use only
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Otherwise if it is for modification time then use like as follows
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
In my case works like this:
In phpMyAdmin:
ALTER TABLE `name_table` ADD `name_row` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
In PHP sintax for the row:
date('Y-m-d H:i:s')