MySQL now() change timezone - php

I'm using the following INSERT statement:
INSERT INTO messages SET `to` = '".$to."', `from` = '".$this->userid."', `title` = '".$title."', `message` = '".$message."', `created` = NOW()
However, it uses my server time (America/Montreal). I want time zone of Asia (Asia/Calcutta)
Is this possible with the same query?

Better use the SQL format directly in your query:
..`created` = CONVERT_TZ(NOW(),'SYSTEM','Asia/Calcutta')..

You would want to go ahead and use the CONVERT_TZ() function in MySQL. It's based off the Olson database which your operating system uses.
Here is the documentation.

After you open the connection to MySQL, run the following as a query:
SET time_zone = timezone;
Then all functions you do will run for that timezone for that connection (i.e. until you close the "link" to the database".
If you have the appropriate permissions you can lock it "permanently" / globaly. Tiemzone strings are standard strings as you have in your question.
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

$myDateTime = new DateTime('2012-05-23 17:01', new DateTimeZone('GMT'));
$myDateTime->setTimezone(new DateTimeZone('Asia/Kolkata'));
echo $myDateTime->format('Y-m-d H:i');
After modification to above code, such as desired format; you could use $myDateTime variable to insert into database.

Related

How to explicitly convert date to string date in php for entering date field in oracle?

When I try to insert a record in to the oracle using oci8 extension, I'm getting invalid month error.
'29-12-2015'
But if I try:
'29-DEC-2015'
It works. What could be the problem? And how can I solve it?
Unless you've explicitly set your session parameters you're probably using the database default date format; something like DD-MON-RR.
Check your session date formats using select * from nls_session_parameters.
To address your problem you can either:
set your session to recognise the date format you want to use:
alter session set NLS_DATE_FORMAT = ...
or explicitly convert strings to dates in the statement:
... to_date('2015-01-13', 'YYYY-MM-DD') ...

MySQL update DATETIME to NOW() using PHP

This all worked before I added the section to update "last_update".
if((time() - $last_update) > 7200){
$sql = $dbh->prepare("UPDATE item_list SET quantity=:quantity, price=:price, last_update=:now WHERE item_name=:itemname");
$sql->bindParam(':quantity', $json->volume);
$sql->bindParam(':price', $json->lowest_price);
$sql->bindParam(':itemname', $row['Item_Name']);
$sql->bindParam(':now', "NOW()"); //This doesn't work
$sql->execute();
}
When this is called I want to make last_update the date and time now. In the database it is currently a DATETIME, and when I last_update I origianly set them to NOW();
Doing this I get the error Fatal error: Cannot pass parameter 2 by reference in.... Directory
I know it expects a variable, I'm not sure how to fix it though. I tried setting
$now = "NOW()";
$sql->bindParam(':now', $now);
No prevail. Any help?
Why you need to bind, just put NOW() directly
$sql = $dbh->prepare("UPDATE item_list SET quantity=:quantity, price=:price, last_update=now() WHERE item_name=:itemname");
If your last_update column is looking for a UNIX timestamp, then do :
$now = time();
$sql->bindParam(':now', $now);
If it's after a different time format, use date(), and the relevant formatting it has to set the date and time
You can keep your bind query as it is & remove the last_update column from the query.
Since you are updating other things in the record via another query, then you can set the default value of the field last_update to CURRENT_TIMESTAMP & set it's attribute as ON UPDATE CURRENT_TIMESTAMP. That will ensure it automatically updates itself with the current time now() whenever that record is updated.
It wouldn't be the best thing to remove bind() as you rightly said to prevent SQL injection attempts.

oci_execute(): ORA-01840: input value not long enough for date format

I am currently receiving the following error message from oci in php (I've been on it for a few hours).
oci_execute(): ORA-01840: input value not long enough for date format
It strange, because when I run the query within SQL Developer it seems to work fine.
This makes me think that when I bind the parameter it is turning the dates into a type that is not able to calculate using conventional operators in oracle.
$startDateTime = '2015-03-06 00:00:00';
$endDateTime = '2015-04-06 00:00:00';
$value = '20';
$type = '$';
$SQL = "SELECT count(*) AS \"COUNT\"
FROM bonus where value = :d_value
AND TYPE = :d_type
AND ((:d_valid_from between valid_from AND valid_till) OR (:d_value_till between valid_from AND valid_till) OR (:d_valid_from < valid_from AND valid_till < :d_valid_till))";
$this->stmnt = $this->conn->prepare($SQL);
$this->stmnt->bindParam('d_valid_from', $startDateTime);
$this->stmnt->bindParam('d_valid_till', $endDateTime);
$this->stmnt->bindParam('d_value', $value);
$this->stmnt->bindParam('d_type', $type);
$this->stmnt->execute();
I am unable to find many resources that deal with php directly with this problem in hand. Does anybody have any experience with it?
I think that your dates are being bound as strings in the query. Assuming that the columns you are comparing it to (e.g. valid_from) are dates, then the string value is being converted to a date using the default date format for the sessions. The difference in behavior in SQL Developer is probably because the default format is different.
Anyway, the solution is to follow a very simple and important rule, which is not to rely on default type conversion. Explicitly convert the string to a date in your query, specifying the appropriate format:
TO_DATE( :d_valid_from, 'YYYY-MM-DD HH24:MI:SS' )

Compare Date in sql table to current date

I am working on a project in which the dates in the sql table needs to be checked with current date. If the ticket goes past the current date, then the status of ticket go from Active to Expired.
I am not good at php. This is what I came up with. I wrote this function at top of the page so that each time the page loads, it checks for the date and compares. date format is yyyy-mm-dd.
What am I doing wrong. Can anyone please help me out?
$result= "SELECT date, status FROM TABLE1";
while($row = sqlsrv_fetch_array($result)){
if(strtotime($row['date']) > strtotime(date('Y-m-d'))){
$updatequery = " UPDATE TABLE1 SET $row[status] = 'Expired' ";
}}
I would advise using the PHP DateTime class it has the date diff function so you could implement like this
$today = new DateTime('today');
$expires = new DateTime($datefromdb);
$diff = $today->diff($expires);
if($diff < 1)
{
$updatequery = " UPDATE TABLE1 SET $row[status] = 'Expired' ";
}
You can use
$result= "SELECT UNIX_TIMESTAMP(date) AS unixdate, status FROM TABLE1";
and then compare
if ($row['unixdate']) > strtotime(date('Y-m-d')))
Your sql database may be on a server whose time is different from the time on the machine where you are running the code, so I would recommend doing the check and update all on the sql server side.
(Disclaimer: I use mysql, so that's how I've written my answer. I assume you can translate to whatever sql database you use)
I would recommend using MySql's date functions, which you can see here.
UPDATE TABLE1 SET status='Expired' WHERE DATEDIFF(CURDATE(), date) < 0
Below code working for me, just a single line of update query will updated less than of current date
UPDATE TABLE_NAME SET status='expired' WHERE DATEDIFF(date, CURDATE()) < 0
Thank.

Error when trying to insert date into datetime (php & SQL Server stored procedure)

I have the same problem but a little different from Neil problems (Error when trying to insert date into datetime column),
Neil want to get now date time so he can use CURRENT_TIMESTAMP or GETDATE().
My question is, how if I have string of date like:
$date = '2011-03-29';
then I want to insert into SQL Server database using a stored procedure with PHP.
insert into tbl(date)
values($date)
Can anybody help? Thanks
Just fyi, I have
SQL Server table:
CREATE TABLE tbl
(
date datetime
)
Stored procedure:
create procedure sp_insertdate
(#date datetime)
as
begin
insert into tbl(date) values(#date)
end
PHP code:
<?php
$date = '2011-03-29';
include("con.php");
$query = mssql_init("sp_insertdate");
mssql_bind($query, "#date", &$date, SQLINT4); //sqlint4 for datetime
mssql_execute($query);
?>
The result is 1900-1-1,
I have tried to send the varchar (in php) first then convert to the datetime (in stored procedure) but there is some error.
Really I'm stuck.. any idea for this problem?
I just want to insert string 2011-03-29 into a SQL Server datetime column using a stored procedure from PHP. I'm sorry because I can't speak English fluently
Whenever I've had a problem like this (using mysql), I just had to add the time (midnight) onto the date so that it can recognize it as the datetime that it's expecting. Try this:
$date = '2011-03-29';
$date .= ' 00:00:00';
Then process the rest as you would. This works for mysql, maybe sql-server needs it like this too.
i got answer from my friends,
just put
mssql_bind($query, "#date", &$date, SQLCHAR); //not sqlint4 for datetime but just sqlchar or sqlvarchar
however many thanks to Groovetrain, i can put also
$date='2011-03-29 00:00:00'
then put
mssql_bind($query, "#date", &$date, SQLCHAR,false,false,19); //19 length of date string

Categories