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
Related
Say you're building a web application like a message board, in which you want to record the time when a message is submitted. There's no need to further process the date in any way. Is there any practical difference between getting the date in the application itself or generating it in the database? Is one method recommended over the other? If so, why?
$msg = "Stuff";
//Generating the date in the application
$currentTime = date("Y-m-d H:i:s");
$st = $mysqli->prepare("insert into messages (msg, date_time) values (?, ?)");
$st->bind_param("ss", $msg, $currentTime);
//--- OR ---
//Generating the date in the database
$st = $mysqli->prepare("insert into messages (msg, date_time) values (?, CURRENT_TIME())");
$st->bind_param("s", $msg);
Also, would the second option be better implemented by using a default value of CURRENT_TIMESTAMP in the date_time field or it doesn't make much difference?
If the database and the web server are located on a single server, it won't matter. Their times would be the same.
You can either:
save the datetime under UTC+0
save the timestamp by using PHP's time()
and let the application perform time zone adjustment and/or timestamp to datetime conversion.
I have this SQL PHP code where I insert the NOW() time:
$sql = "INSERT INTO logs (username, msg, color, Time) VALUES ('$uname', '$msg', '$color', NOW())";
Unfortunately, it gives me GMT rather than Central European Time. What can I do? I tried putting date_default_timezone_set('Europe/Vienna'); Before it but it won't work.
Get the time with PHP and insert inside the database. Servers time is diffrent as they may located at diffrent locations. In your case the value of timestamp will be inserted by the server.
date_default_timezone_set('Europe/Vienna');
$time = date('d-m-Y H:i:s');
$sql = "INSERT INTO logs (username, msg, color, Time) VALUES ('$uname', '$msg', '$color', '$time')";
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());";
When my html form is submitted, I want today's date also stored in one column along with the data given by the form in mysql table.
My php code:
#some code
$date = date('d-m-Y');
#some code
$sql = "INSERT INTO table1(rollNo, password, name, item, place, description, contact, date) VALUES('$rollNo', '$password', '$name', '$item', '$place', '$description', '$contact', '$date') ";
But for some reason, every time form is submitted, in the date column '0000-00-00' is stored instead of today's date. I tried using different formats(d/m/Y etc.), but didn't work. I have checked that in MySQL table, date column's type is date, not string. I am a newbie in php and MySQL and I don't know why this is happening.
Also, I want this page to daily(at 11:59 PM) send mail of that day's entries. For that, I am planning to check every entry's date with today's date, and send mail of only those that match. Please tell me if there is another simpler method of doing it.
EDIT:
Just to make it clear, date column's type is DATE.
Use:
`dateandtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
And using a simple substring, you can pick out the date (YYYY-MM-DD) with ease.
substr($sql['dateandtime'], 0, 10);
Use NOW() function of MySQL with column type DATETIME
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_now
You can also get just the date:
http://www.w3schools.com/sql/func_curdate.asp
UPDATE table SET date = CURDATE();
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.