phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

Camlquery returns user local datetime instead of UTC datetime

Open kevinlaucw opened this issue 4 years ago • 0 comments

Hi,

Camlquery returns user local datetime instead of UTC datetime when retrieving a datetime (date only format) field from SharePoint.

SharePoint field name: my_date Date and Time format: Date only

PHP 8.1.10

Camlquery returns user local datetime instead of UTC datetime

$view_xml = "";
$view_xml = $view_xml."<View>";
$view_xml = $view_xml."  <Query>";
$view_xml = $view_xml."    <Where>";
$view_xml = $view_xml."		 <Eq><FieldRef Name='ID' /><Value Type='Counter'>".$id."</Value></Eq>";
$view_xml = $view_xml."    </Where>";
$view_xml = $view_xml."  </Query>";
$view_xml = $view_xml."</View>";

$qry = new CamlQuery();
$qry->ViewXml = $view_xml;
$list = $client->getWeb()->getLists()->getByTitle($list_title);
$items = $list->getItems($qry);
$client->executeQuery();
foreach ($items as $index => $item) {
	echo $item->getProperty('my_date');
}

getItemById returns UTC datetime

$list = $client->getWeb()->getLists()->getByTitle($list_title);
$item = $list->getItemById($id);
$client->load($item);
$client->executeQuery();
echo $item->getProperty('my_date');

I have tested both scenarios in C# and they both returned UTC datetime:

using (var context = new ClientContext(list_url))
{
    var securePassword = new SecureString();

    foreach (char c in SharepointPassword)
        securePassword.AppendChar(c);

    context.Credentials = new SharePointOnlineCredentials(SharepointLogin, securePassword);

    int sid = 29;
    List targetList = context.Web.Lists.GetByTitle(list_title);
    ListItem targetListItem = targetList.GetItemById(sid);
    context.Load(targetListItem);
    context.ExecuteQuery();
    Console.WriteLine("Retrieved item is: {0}", targetListItem["my_date"]);

    CamlQuery query = new CamlQuery();
    query.ViewXml =
        "<View>" +
        "   <Query>" +
        "      <Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + sid + "</Value></Eq></Where>" +
        "   </Query>" +
        "</View>";

    ListItemCollection collListItem = targetList.GetItems(query);
    context.Load(collListItem);
    context.ExecuteQuery();

    foreach (ListItem myListItem in collListItem)
        Console.WriteLine("Retrieved item is: {0}", myListItem["my_date"]);
}

Please advice. Thank you.

kevinlaucw avatar Nov 14 '22 23:11 kevinlaucw