0

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

Tomasz
  • 4,847
  • 2
  • 32
  • 41
Patweb
  • 133
  • 1
  • 10
  • If you don't want the timezone (those aren't milliseconds), then don't use a date format that contains a timezone. Use [formatting characters](http://php.net/manual/en/function.date.php) to get the correct format to being with, rather than manipulating a different one. – iainn Oct 24 '18 at 11:48
  • 1
    so i guess you dont really want ISQ8601, ie what you want is not what you ask. AND +03:00 is NOT milliseconds, it is offset from 'Z' . Read **[this about ISO8601](https://en.wikipedia.org/wiki/ISO_8601)** – YvesLeBorg Oct 24 '18 at 11:48
  • @John Conde ,, It is not a duplicate,, I need to remove the last characters which is a different question from the other one.. – Patweb Oct 24 '18 at 11:49
  • @YvesLeBorg Okay,, how do I remove it,, I am using that date format to hit an API which requires a strict format – Patweb Oct 24 '18 at 11:50
  • @Patweb Yes it is. You just want to change the format of a date and that question explains how to do that. Basically, don't use the `c` formatter. – John Conde Oct 24 '18 at 11:56
  • why dont you post the spec of that so-called 'strict' API date format ? you might get more appropriate guidance. Seems to me this is an **[XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)**. – YvesLeBorg Oct 24 '18 at 11:58
  • @JohnConde Okay,, thanks – Patweb Oct 24 '18 at 12:00

1 Answers1

1

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'))