Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date toIsoString PHP [duplicate]

Tags:

date

php

datetime

I want to have a date like new Date().toISOString() of javascript

The output is 2018-06-11T08:30:25.597Z

I tried with

(new \DateTime('now',new \DateTimeZone("UTC")))->format(\DateTime::ISO8601) // 2018-06-12T08:21:13+0000


$t = date('c', strtotime('2010-12-30 23:21:46')); //2010-12-30T23:21:46+01:00
$t2 = date(DATE_ISO8601, strtotime('2010-12-30 23:21:46')); //2010-12-30T23:21:46+01:00

$datetime = new \DateTime('2010-12-30 23:21:46');
$t3 = $datetime->format(\DateTime::ATOM); // 2010-12-30T23:21:46+01:00

I want to combine

Combined ISO 8601 date and time in UTC (YYYY-MM-DDTHH:MM:S+Timezone Offset|Z, i.e., 2018-04-18T11:02:05.261Z)

In Javascript I can have this format with

new Date().toISOString() //2018-06-12T08:24:49.321Z
like image 588
monkeyUser Avatar asked Nov 07 '22 05:11

monkeyUser


1 Answers

Your first try is almost what you need, just change \DateTime::ISO8601 to \DateTime::ATOM.

From PHP manual:

DateTime::ISO8601 DATE_ISO8601

ISO-8601 (example: 2005-08-15T15:52:01+0000)

Note: This format is not compatible with ISO-8601, but is left this way for backward compatibility reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead.

like image 94
Łukasz Jakubek Avatar answered Nov 12 '22 11:11

Łukasz Jakubek