Change System.DateModified format on Windows Searc

2019-03-02 15:20发布

I'm using Windows Indexing search together with PHP to search inside thousands of files.

I got it working by using the PHP COM class:

$conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$recordset = new COM("ADODB.Recordset");

$conn - > Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';");


$recordset - > Open("SELECT System.ItemName, System.DateModified FROM  SYSTEMINDEX  WHERE DIRECTORY='file:C:/xxxx/' AND CONTAINS('xxxx')", $conn);

$recordset - > MoveFirst();

while (!$recordset - > EOF) {
    echo $recordset - > Fields - > Item("System.ItemName") - > Value."\n";
    $recordset - > MoveNext();
}

I am retrieving the DateModified field of each element and I've realized the format of it depends on the System configuration. It is possible to set it through the control panel in Windows.

I am wondering if there's any way to get it in the desired format in order to avoid having to change it by using the control panel in every system I want to run the script.

I noticed the object has the property formatAs but I'm not sure how to change it and the documentation is not very complete.

Thanks.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-02 16:12

You can convert a VT_DATE object into a UNIX timestamp with variant_date_to_timestamp and then format it with date, this should work regardless of the date format in the control panel.

$format = "Y-m-d";
$object = $recordset->Fields->Item("System.DateModified")->Value;
$timestamp = variant_date_to_timestamp($object);
echo date($format, $timestamp) . "\n";
查看更多
登录 后发表回答