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());";
Related
My site hosting in filezilla server always saves wrong time in timestamp field in the mysql database..I checked my code in localhost it saves correct time clearly..Timestamp field query is below here
$add_SQL = "insert into applicant (name, createdon) values ('$name', now())";
Is there anything in the filezilla config affects it?
Please help me..Im hanging for 6 days.
If you have admin rights on your database you can change the timezone in the database
Otherwise you can do this with PHP. At the top of your script you should set the timezone, this will ensure that all date/time operations return values related to that timezone.
Then use PHP time() function in your query.
e.g.
date_default_timezone_set('America/Los_Angeles');
...
$add_SQL = "insert into applicant (name, createdon) values ('$name', ".time().")";
Ensure you provide a valid timezone: See list of all possible timezones
I am developing a website in which on particular event on the website $query is executed and entry is made in the db in user_id and store used and order_date Field .Order_date is of type datetime in which current timestamp value gets automatically inserted upon entry.But to the dismay the server is hosted in US so the time getting inserted is of US,so i wanted it to be in the ist format and also when i am retrieving the values i want to format it such that only time appears.Have googled a lot but none of them worked.Please Help
<!--Insertion Query -->
$querye = "INSERT INTO orderdata(user_id, store_used) ".
"VALUES ('$coola', 'Airtel')";
<!--Retrieval Query -->
'<td align="center">'.$row['order_no'].'</td>'.
'<td>'.$row['order_date'].'</td>'.
'<td>'.$row['store_used'].'</td>'.
'<td>'.$row['status'].'</td>'.
'<td>'.$row['cashback'].'</td>'.
'<td>'.$row['est_cashback_date'].'</td>'.
i wanted order_date to be in the IST format
You can set time_zone per connection basis to a specific timezone like IST as Documented Here. probably you can also do the same using PHP but not sure about that.
SET time_zone = 'IST';
upon retrieval format datetime to date only in php?
You can use DATE(expr) function to get only the date part like
select date(order_date) as order_date
from orderdata;
Per your commented SQL, you can do it like
SELECT i.order_no,
date(i.order_date) as order_date,
i.store_used,
i.status,
i.cashback,
i.est_cashback_date
FROM orderdata as i
WHERE i.user_id =$a
ORDER BY i.order_date DESC
$limit
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
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've set a field as date in xammp server. but when i'm inserting a values, its saving like 0000-00-00.. i've even input it manually like 2010-10-10 but still saveing 000... is the problem with my code or the xammp server??? or is there any way to configure the date format in xammp???
$today = date('Y-m-d');
"/>
update.php
$date = $_GET['datee'];
$qry = "INSERT INTO course_detail(userid, course_id, hours_complete, week_no, date) VALUES('$member_id','$fname','$hour','$week', '$date')";
$result = #mysql_query($qry);
It's probably with your code. Have you quoted the date string when inserting?
INSERT INTO mytable SET datefield='2010-10-10'
This is definitely not a XAMPP issue. I highly recommend you take a look at this link MySQL Documentation on Datetime
MySQL Date has a fixed syntax and it generally falls along the lines of YYYY-MM-DD or YYYYMMDD. I'm sure you're using a presentation layer over MySQL such as *.NET (ASP,winforms), you can reformat the date generated by MySQL to match your locale. Here's how you do it on the Microsoft stack MSDN Globalization Step-by-Step, similar methods are available for whatever else technology you may be using.