I am trying to convert a given date and time in format ( yyyy-mm-dd hh:mm:ss ) to (yyyymmddThhmmssZ) in php. For example: 2020-07-30 18:30:00 should be 20200730T183000Z. I also want to know if there are any existing/in-built methods to accomplish this?
Asked
Active
Viewed 183 times
1
-
ha. the clue is already in one of your tags. php's `DateTime` – Kevin Jul 21 '20 at 07:27
-
Have you made any effort at all to solve this problem yourself yet? – Tim Biegeleisen Jul 21 '20 at 07:27
-
I want to know if there are any existing methods to perform this – Neeraj Malwal Jul 21 '20 at 07:28
-
yep, there are existing methods to perform this [`DateTime`](https://www.php.net/manual/en/datetime.format.php) – Kevin Jul 21 '20 at 07:29
-
1Does this answer your question? [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – GNassro Jul 21 '20 at 07:36
1 Answers
0
As others have said in your comments, you can simply do this with the DateTime
object
Here's an example:
$date = '2020-07-21 18:50:32';
$obj = new \DateTime('@' . strtotime($date));
echo $obj->format(DATE_RFC3339); // 2020-07-22T01:50:32+00:00
If RFC3339 isn't your preferred format, feel free to pick one from the manual, or build your own
Try it out: http://sandbox.onlinephpfunctions.com/code/3246477b676e17022da799697acf4df375e1fe08

zanderwar
- 3,440
- 3
- 28
- 46