0

I have a few dates in my JSON file and I wonder how I can rewrite the output of them.

Current date: 2018-03-04T23:40:46.94 How it should be: 04/03/2018

Current JSON:

 {
    "SKU": "PQ-70-7EF",
    "ParentSKU": null,
    "Category": "Alarm clocks",
    "EAN": "4971850420019",
    "Title": "Casio Wakeup timers",
    "Description": "Casio Wakeup timers",
    "Brand": "CASIO",
    "RRP": 56,
    "Price": 26.1,
    "PriceUpdateUTC": null,
    "Stock": 0,
    "StockUpdateUTC": "2018-03-04T23:40:46.94",
    "Images": [
        {
            "ImageSort": null,
            "ImageURL": "https://uwt.blob.core.windows.net/dsales/partimage/pq-70-7ef_kniwoz.jpg",
            "IsDefault": true
        }
    ]
}

Current script:

foreach ($data as $key => $entry) {
    $data[$key]['StockUpdateUTC'] = date('d/m/Y');
}

The above script is changing all dates to today's date. Thanks for the help.

Regards Ely

1
  • Not certain why you would want to change that. When it comes to dates, either persisting them or sending on a wire in some kind of API, you should prefer UTC (as in your actual format). Commented Mar 5, 2018 at 14:27

2 Answers 2

2

You have to convert the current string date to a timestamp (using strtotime()) and then pass this timestamp as the second argument of date().

foreach ($data as $key => $entry) {
    $data[$key]['StockUpdateUTC'] = date('d/m/Y', strtotime($data[$key]['StockUpdateUTC']));
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can pass the date variable as a parameter in the date() function, like this:

foreach ($data as $key => $entry) {
    $data[$key]['StockUpdateUTC'] = date('d/m/Y',strtotime($data[$key]['StockUpdateUTC']));
 }

This way it will convert your date, instead of using the current one.

EDIT: As mencioned by Syscall, u need to convert your string to a actual date, using strtotime()

4 Comments

This is correct, but why not link the date() reference to the PHP documentation for reference?
This changes the date for all to: "StockUpdateUTC": "01/01/1970".
Need to use strtotime here like Syscall.
Yeah i forgot about that, apologies.

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.