2

PHP - Access MSSQL datetime column from the returned array

Array
(
    [0] => Array
        (
            [CallId] => 45
            [CallLoginId] => 1
            [CustomerId] => 140
            [CallOptionId] => 2
            [CallTypeId] => 2
            [CallStatusId] => 1
            [CallDateTime] => DateTime Object
                (
                    [date] => 2012-06-28 00:00:00
                    [timezone_type] => 3
                    [timezone] => Asia/Kolkata
                )

            [ContactNo] => 45151551115
            [ContactPerson] => Contact Person name
            [ProductId] => 1
            [ProdCompanyId] => 1
            [ProdCategoryId] => 1
            [ModelNo] => 451212151
            [ProdUnderId] => 1
            [Problem] => Simple Problem Details 
            [Remarks] => Remarks 
            [Accessories] => Accessories 
            [CallCaseId] => 
            [CallCaseDate] => 
            [ServiceCharge] => 0
            [CourierName] => 
            [DocketNo] => 
            [CompanyId] => 126
            [ASPId] => 130
            [InsBy] => 134
            [InsDate] => DateTime Object
                (
                    [date] => 2012-06-23 17:04:51
                    [timezone_type] => 3
                    [timezone] => Asia/Kolkata
                )

            [UpdBy] => 11
            [UpdDate] => DateTime Object
                (
                    [date] => 2012-06-28 18:29:23
                    [timezone_type] => 3
                    [timezone] => Asia/Kolkata
                )

            [FName] => Kumar
            [MName] => a
            [LName] => Customer
            [ProductName] => LenovoDesktop420
            [CallOption] => InHouse
            [CallType] => H.W.Installation
            [ProdCompany] => Lenovo
            [ProdCategory] => Desktop
            [ProdUnder] => AMC
            [CallStatus] => Open
            [EntityId] => 134
            [InsertBy] => Bhavin   Rana
        )

)

how can access date time value form this returned array ? thanks in advance.

1 Answer 1

2

You have an outer array with numerically indexed one element [0], which is an associative array. CallDateTime as a key of that array is a DateTime object.

$array[0]['CallDateTime']->format('Y-m-d H:i:s')  // 2012-06-28 00:00:00
$array[0]['CallDateTime']->getTimezone()->getName()    // Asia/Kolkata

// Same with InsDate and UpdDate
$array[0]['InsDate']->format('Y-m-d H:i:s')         // 2012-06-23 17:04:51
$array[0]['InsDate']->getTimezone()->getName()           // Asia/Kolkata
Sign up to request clarification or add additional context in comments.

7 Comments

Fatal error: Cannot use object of type DateTime as array in C:\inetpub... where echo $this->CallDetail [0]['ContactNo']; works fine.
@BhavinRana Oops sorry I misread the data structure. Those are objects and therefore you need to access their properties via ->
tried with echo $this->CallDetail[0]['CallDateTime']->date ; Notice: Undefined property: DateTime::$date in C:\inetpub\...
var_dump($this->CallDetail[0]['CallDateTime']). The property is clearly there from your structure above.
above var_dump = object(DateTime)#85 (3) { ["date"]=> string(19) "2012-06-28 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(12) "Asia/Kolkata" }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.