0

I'm accessing an mssql db in PHP and creating the connection as a new COM() object. My query runs perfectly fine. When I do the query in Microsoft SQL Server 2008, everything looks great, the dates/times show up in columns and are readable.

But when I run the same query and return the results in PHP, I go to iterate through the results like so:

$selected_fields = array("FS.fund_name", "M.media_id", "MT.media_type_name", "modified_date", "file_upload_date", "issued_date", "filename");
//query goes here
$record_set = new COM("ADODB.Recordset");
while(!$record_set->EOF){ //loop through query results and add to inline cache variable
  $temp_record = array();
  foreach($selected_fields as $fieldname){ //go through fields and add to array
      $fieldname_array = explode(".", $fieldname); //separate table and column
      $fieldname = $fieldname_array[sizeof($fieldname_array)-1];
      $temp_record[$fieldname] = $record_set->fields[$fieldname]->value;
  }
  print_r($temp_record);
  $rs_array[] = $temp_record;
  $record_set->movenext();
}

As I print each $temp_record, they end up looking like this:

Array ( [fund_name] => TEST [media_id] => 1001 [media_type_name] => 8937 Tax Forms [modified_date] => variant Object [file_upload_date] => variant Object [issued_date] => variant Object [filename] => form8937test.pdf )

So the dates are some kind of object, and I don't know how to access them in PHP. And when I go to save them as a cache file, they have this value:

variant::__set_state(array())

How can I retrive the date/time in a format that I can then manipulate using PHP? I want to filter them by year, and sort them by most recent.

4

1 回答 1

1

根据Variant Object文档,您应该可以通过

$temp_record['modified_date']->value; 

或者

$val = (string)$temp_record['modified_date']; 

来自同一个文档:

仅当类型之间存在不会导致信息丢失的直接映射时,变量才会转换为 PHP 值。在所有其他情况下,结果将作为 VARIANT 类的实例返回。

因此,要检查是否需要强制转换,只需检查您是否有一个Variant类对象:

if (is_object($var) && $var instanceof Variant) {
    $val = (string)$var;
} else {
    $val = $var;
}
于 2013-07-22T20:23:43.073 回答