Home > Net >  Unable to convert DateTime timestamp to m/d/y format
Unable to convert DateTime timestamp to m/d/y format

Time:02-01

So I am using an API and I am getting the following updatedOn response: "UpdatedOn": "/Date(1674542180860 0000)/".

I am using the following code to convert it to m/d/y format:

if (!empty($data->Data->Shipment->UpdatedOn)): 
    $timestamp = preg_replace('/[^0-9 -]/', '', $data->Data->Shipment->UpdatedOn);
    $date = new DateTime("@$timestamp");
    $timestamp = $date->getTimestamp();
    $parse_date = date('m/d/Y', $timestamp);
    echo '<span>' . $parse_date . '</span>';
endif; 

The issue is that it's not outputting properly, so I need some help - Here is the output that I am receiving:

02/17/55034

I would like to output it in the following format: January 12th, 2023.

Could anyone help me with my code?

CodePudding user response:

In our decade timestamp is a 10 digit number So this nember => "1674542180860" must be devide to 1000, And if these two numbers "1674542180860" and "0000" must be added together, we should explod this string and sum two number, I suggest you try this:

if (!empty($data->Data->Shipment->UpdatedOn)){
            $timestamp = preg_replace('/[^0-9 -]/', '', $data->Data->Shipment->UpdatedOn);
            $timestamp = explode(' ',$timestamp);
            $timestamp = (int)$timestamp[0]   (int)$timestamp[1];
            $timestamp = (int)($timestamp/1000);
            $parse_date = date('F d\t\h Y', $timestamp);
            echo '<span>' . $parse_date . '</span>';
        }

This is your favorite format to show date => "F d\t\h Y"

CodePudding user response:

if(!empty($data->Data->Shipment->UpdatedOn)){ $parse_date = date('M d, Y', strtotime($data->Data->Shipment->UpdatedOn)); echo '<span>' . $parse_date . '</span>'; }

  • Related