Search code examples
phpdate-formattingiso8601

Error converting a date to ISO 8601 in PHP and remove milliseconds


Am working on a date input in PHP whereby I am converting to ISO 8601 which is working fine. It is working fine but I need to remove the last characters which are in milliseconds

PHP code to convert to ISO 8601

//Getting the timezone
date_default_timezone_set('Africa/Nairobi');

//CONVERTING TO ISO 8601
$newDate = date('c', strtotime('+24 hours'));
dd($newDate);

//Output after dd
2018-10-25T14:34:44+03:00

I need to remove +03:00 so that the output will be 2018-10-25T14:34:44


Solution

  • How about substring?

    substr(date('c',strtotime('+24 hours')),0,-6)
    

    Or, just format as required:

    date('Y-m-d\TH:i:s',strtotime('+24 hours'))