How to set time to Central European timezone - php

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

Related

STR_TO_DATE error that has correctly formatted DATA

I am working on a school website that was working but is now having errors. The page attempts to insert a new exam into our database of exams, but on submission hits this error.
"Error SQLSTATE[HY000]: General error: 1411 Incorrect datetime value: '2:50 AM' for function str_to_date"
The code we are using to input the data is as follows
$sqlInsertExam = "INSERT INTO exam (name, quarter, date, location, state, possible_grade, passing_grade, duration, start_time, cutoff)
VALUES (:name, :quarter, :exam_date, :location, :state, :possible_grade, :passing_grade, :duration, STR_TO_DATE(:start_time, '%h:%i %p'), :cutoff)";
as you can see we are using %h:%i %p which as far as I know means in 12 hour format hr:min AM/PM. Which is what we are giving it. The only problem I can see is that the previous datetimes in the table are listed as military time in the form hr:mm:ss e.g. 14:00:00. But I am unsure if that is what is causing this error

Filezilla Server gives wrong time

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

Date/Time Field In Mysql table

I have the following code in php that inserts a start date and end date with the date and time . The date and time is added via the iphone date and time wheel when selecting the input field. The problem however is that if I select 1pm local time as the start date and 2pm as the end date it will submit the record as 6pm and 7pm. Is this a coding issue, a date/time field issue or a form issue? The variables of $start_date and $end_date are pulled directly from the input form.
$query = "INSERT INTO events (event_id, event_name, user_name, id_user, start_date, end_date, details, location, dresscode,private) VALUES ('NULL', '".$event_name."', '".$user_name."', '".$user_id."', '".$start_date."','".$end_date."','".$time."','".$location."','".$dresscode."','".$private_id."')";
mysql_query ($query);

Storing the current date/time

I have a site where users can update their status. Knowing the date/time they posted the status is very important. I create a field in my table and gave it the date time type.
I am new to working with dates/times. I know the structure for the datetime field is this:
YYYY-MM-DD HH:MM:SS
I thought doing this in my INSERT query would work, but It didnt.
$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story',
'date()', '1')";
When I check the database table the post is successful, but the datetime reads
0000-00-00 00:00:00
What can I do to achieve my end result. This whole unix time stamp and stuff with date/time really confuses me. Thanks.
Don't quote the mysql function, and you should use NOW() instead of DATE().
$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story', NOW(), '1')";
Give this a try, this will add the record to the database with the current date and time in the format you posted:
$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story', now(), '1')";
EDIT: Posted same time as above.
Lets tr this:
$date_time = NOW();
then
$query= "INSERT INTO posts (user_id, story, date, view) VALUES ('$user_id', '$story',$date_time, 'My_View')";

Save date/time from PHP to SQL

I want to save the date and time from PHP to SQL. Here is the SQL statement to insert new record (found in a method within a class):
INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
value (:headline, :text, :date, :rating, :product_id, :username)
And in my .php page, I call the current date and time using $_SERVER['REQUEST_TIME']. But still I'm getting the error "Incorrect datetime value". What can I use to get the date?
Your timestamp can be generated:
$timestamp = date('Y-m-d H:i:s');
This should mimic the mysql timestamp and datetime formats.
Assuming that the mysql has its timestamp synchronized with the php server in question, you can also just use the mysql current timestamp functions:
NOW() or CURRENT_TIMESTAMP or CURRENT_TIMESTAMP()
Does it have to be the exact request time? You could make your life easier and simply use:
INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
value (:headline, :text, now(), :rating, :product_id, :username)
MySQL inserts the current date as soon your entry is written to the table.
it is only a guess, but $_SERVER['REQUEST_TIME'] returns a float, not a valid datetime format.
If you need the current time you can do it like that:
INSERT INTO tbl_reviews (headline, text, date, rating, product_id, username)
INSERT INTO tbl_reviews ('$headline', '$text', CURDATE(), '$rating', '$product_id', '$username');
date_default_timezone_set('US/Eastern');
$cur_date=date("Y-m-d");
You can use SQL's own CURRENT_TIMESTAMP value to get an automatically formatted timestamp.
As for $_SERVER['REQUEST_TIME'], you can just use time() or microtime() instead.
As mentioned, REQUEST_TIME, as well as time() and microtime() return a UNIX timestamp, which is basically the amount of seconds that have passed since the UNIX epoch, which is not the same format as a DATETIME field expects.

Categories