I have two Windows 2008 servers with IIS, PHP and MSSQL.
In a PHP-script, I query a table with a field of type 'datetime':
SELECT timestamp FROM table
Now the problem is, that on one server I get this format:
2012-02-05 12:32:54.020
But on the other server I get this (which I donĀ“t want):
Feb 5 2012 12:32PM
When querying through SQL Server Management Studio I get the 'correct' (2012-02-05 12:32:54.020) value on both servers.
My question is:
How do I disable this conversion in PHP so that the format returned is always in this format: 2012-02-05 12:32:54.020?
Edit: I know that I could change the script to format the date, but I don't want to go through 10000+ files. What is causing this conversion? Is it PHP, SQL or IIS? It is working on the old server, so I assume it could work on the new server.
In PHP, you can format the date with date_format like this:
$correctDate = date_format($dateToFormat, "Y-m-d H:i:s");
You may format it on server side:
SELECT CONVERT(varchar, timestamp, 121) FROM table
Related
I get this error message:
The conversion of a nvarchar data type to a datetime
when I try to bind a MySQL datetime variable to a smalldatetime variable in SQL Server statement. The code looks like:
$s = $dbcnx->prepare("SELECT MAX(attr_81577_) AS max_date FROM object_70969_");
$s->execute();
$r = $s->fetch(PDO::FETCH_ASSOC);
$max_date = $r[max_date]; // we got the maximum date from MySQL table
$s = $dbms->prepare("SELECT * FROM orgs WHERE update_date >= :max_date"); // Now we want to filter SQL Server table based on $max_date value
$s->bindParam(':max_date', $max_date);
$s->execute();
What is wrong with that and what can I do?
I think that your SQL Server may be being confused by the non-standard date format you're using. "2015-09-18 16:22:00" is, of course, a pretty reasonable format, but it's not on the list of standard date literal formats for SQL Server.
So, I'd try using the completely unambiguous ISO 8601 format, formatting your dates as "2015-09-18T16:22:00". PHP has a specific format for ISO 8601, "c", but this is, ironically, not compatible with SQL Server's ISO 8601 format. So, try:
date('Y-m-d\TH:i:s', strtotime($max_date));
Note that I'm not completely sure what's going on, as the date format you're using works OK with SQL Server in general, so perhaps this is related to your specific server configuration (dateformat or language setting?) or with PDO; I don't know too much about the PDO SQL Server driver.
Of course, SQL Server is traditionally used with Microsoft client code, which will bind an actual date object to the date parameter rather than a string, and so you'll never have to worry about the formatting. Not sure if there's a way of doing that with PDO.
MySQL expects the date format YYYY-MM-DD, and I'm not sure what the Oracle format you are getting is. You may want to get PHP to translate the Oracle date using PHP's stringtotime function.
As in:
$maxDate = date('Y-m-d', strtotime($max_date));
The strtotime method is a wonderful little function that takes date information in almost any format and tries to convert it into a usable timestamp.
I have many date entries in a VARCHAR field in my MySQL DB.
PHP is formatting them like this:
Sun, 25 Jan 2015 18:20:04 +0000
I know how to insert dates/times into a correct MySQL DATETIME format going forward (once I correct the PHP code), but I want to run a SQL query to convert the existing DB entries.
After much searching on here, I've found many people suggesting something like this:
UPDATE MyTableName SET Date2=str_to_date(Date1, '%Y-%m-%d %H:%M:%S');
However, that seems to just place "NULL" in all of the rows in my new column.
Is this because MySQL cannot handle my particular PHP date format with str_to_date?
If so, can anyone suggest any other ways of updating the field. I don't mind using PHP or MySQL queries to achieve this.
Many thanks.
I have this table
Name Birth_Date Register_Date
---------------------------------------------------------------
Ali 1990-03-22 2010-03-1 15:1:42
Ali1 1991-07-18 2010-03-2 12:44:2
When I inserted these values, I inserted the Birth_Date as a String such as '1990-03-22', and I used 'NOW()' for Register_Date.
NOW() will generate the current datetime according to the MySql Server.
Now when I try to get the time between the current date and the Register_Date (Time passed since he registered), I use the following:
SELECT YEAR(CURDATE())-YEAR(register_date) ...
In PHP, if I wanted to do that, I suppose I have to get the current date: date('Y-m-d H:i:s');
My question is, is there a difference between calculating the date difference (between a date and today) via MySql or via PHP?
Currently, on my localhost (XAMPP), CURDATE() and date(..) generates the same date, but will it generate the same date for other users when my website goes online?
If both your mysql and PHP server are operating on the same timezone and have their clocks properly synchronized, you wont have an issue.
if you want to upload your php website and your DB on the same server I think you'll not have problem , but If you use different servers you may have time issues.
to avoid this issue I advice you to save the time with time zone, to be able to get the correct time from any server.
I've run in to an issue whereby PHP and MySQL seem to disagree on which timezone they should be using when converting certain timestamps back to date and time.
The problem arises when I have a timestamp which I would like to convert back to a datetime format. PHP gives the date and time based on GMT being the timezone, but MySQL seems to think it is operating in GMT+1, and converts accordingly.
Simple reproduction
$original_date = '2009-08-31 23:59:59';
$timestamp = strtotime($original_date);
echo $timestamp; // Gives 1251763199
echo date('Y-m-d H:i:s', $timestamp); // PHP: 2009-08-31 23:59:59
echo db_field('SELECT FROM_UNIXTIME(1251763199)'); // MySQL: 2009-09-01 00:59:59
The timestamp given by PHP seems to be the correct one for the date given, assuming timezone is GMT. The result from MySQL would have been the correct one had we been running BST (timestamp given fell within GMT+1 at that time).
If I try the above with a $timestamp from today (1267640942), PHP and MySQL both seem to be happy to tell me that it is 2010-03-03 18:29:02 (both returning GMT).
What timezone is set on the servers?
I've checked the MySQL docs, which say that if my timezone is set to system than the OS will provide the timezone info. This appears to be the case at the moment:
mysql> SELECT ##global.time_zone, ##session.time_zone;
+--------------------+---------------------+
| ##global.time_zone | ##session.time_zone |
+--------------------+---------------------+
| SYSTEM | SYSTEM |
+--------------------+---------------------+
The default timezone on my web server is GMT. This is also the default system timezone on my database server (according to the date command run on the cl on each server).
foo#bar:/home/foo$ date
Wed Mar 3 18:45:02 GMT 2010
So according to the docs, my DB server should be running on GMT, which is what we want. Yet the query output given in my test scripts suggests that it's running in GMT+1.
I know there are a number of workarounds for this problem (all date arithmetic being done fully in either PHP or MySQL, etc) but I'd love to get to the bottom of what's causing this discrepancy so we can sort it out and prevent anyone else on the team from being tripped up by it.
Does anyone know if there's a very basic setting that I've over-looked here, or know what could be causing this discrepancy?
Thanks!
i use this methodology:
take care of PHP and leave mysql alone
it is recommended to set default timezone for php via date_default_timezone_set php function.
use a TIMESTAMP field type for keeping date record in mySql
then when you insert:
$sDate = date("Y-m-d H:i:s");
mysql_query("insert into `table` set `created_on` = '$sDate' ");
and when you select:
mysql_query("select `created_on` from `table` ");
$iTime = strtotime($aRow['created_on']);
you can always have access to global time using gmdate php function:
$iTime_Global = gmdate("U", $iTime);
the mysql timezone would have no effect in your application if you just take care of your PHP code. (that is made by timezone set)
I would suggest using UTC on your server and MySQL install and convert the timezone(s) to what ever you want. The conversion in MySQL and PHP are fairly simple.
http://www.w3schools.com/php/php_ref_date.asp (getTimezone and setTimezone)
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
Our production platform technologies are these: PHP, MS SQL Sever, and IIS.
PHP is set to the timezone 'Pacific/Auckland' which is current +12:00 GMT/UTC.
When we retrieve dates from SQL Server, they are consistently 12 hours 'behind' what they should be, ie. SQL Server is storing and serving them as GMT dates, even though the time zone on the server itself is Pacific/Auckland too.
Is there a hidden sp function some where you can use to set the timezone? If this is not timezone related, please enlighten me!
Can you open up sql server management studio and type "select getdate()" to make sure it is sql server that is wrong?
Are you using getdate() or another sql function to get the date?
If your statement is both accurate and general, then you should be able to reproduce this simply by inserting a row into a table with a datetime column like this:
CREATE TABLE OffBy12 (
IsThisOff datetime not null
)
GO
INSERT INTO OffBy12 (IsThisOff) VALUES ('2009-06-28')
SELECT IsThisOff
FROM OffBy12
If you're right, then the time portion of the date will not be 00:00.
If you're not correct, then this must be something more complex. In that case, you may want to say how the data are getting into SQL Server.