How do I convert my time from 2010-12-30 23:21:46
to ISO 8601 date format? (-_-;)
Object Oriented
This is the recommended way.
$datetime = new DateTime('2010-12-30 23:21:46');
echo $datetime->format(DateTime::ATOM); // Updated ISO8601
Procedural
For older versions of PHP, or if you are more comfortable with procedural code.
echo date(DATE_ISO8601, strtotime('2010-12-30 23:21:46'));
2010-12-30T23:21:46+1100
how to make it to be 2010-12-30T23:21:46+11:00
? –
Croupier preg_replace('/(?<=\d{2})(?=\d{2}$)/', ':', '2010-12-30T23:21:46+1100')
. It outputs 2010-12-30T23:21:46+11:00
. –
Trahan date('c', strtotime('2010-12-30 23:21:46'))
nice @Parrott :) –
Croupier DATE_ISO8601
produces a date string which is slightly different than ISO8601 (the colon is missing in the TZ, ISO8601 expects times to be all with OR all without the colon, not a mixture) - date('c')
does produces a strict ISO 8601 valid date - This could cause hard to trace bugs if code expects a strict ISO 8601 datetime format. Ref: en.wikipedia.org/wiki/ISO_8601 –
Emulsifier HH:mm
ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 –
Adah DateTime::ATOM
instead, see the note in php.net/manual/en/class.datetime.php#datetime.constants.iso8601 –
Anastomosis DateTimeInterface::ATOM
rather than a child class. –
Ludwick After PHP 5 you can use this: echo date("c");
form ISO 8601 formatted datetime.
Note for comments:
Regarding to this, both of these expressions are valid for timezone, for basic format: ±[hh]:[mm], ±[hh][mm], or ±[hh]
.
But note that, +0X:00 is correct, and +0X00 is incorrect for extended usage. So it's better to use date("c")
. A similar discussion here.
DATE_ISO8601
; +0X:00
vs +0X00
. –
Lotuseater 'c'
and DATE_ISO8601
produce valid ISO8601 time representations. –
Lotuseater DATE_ISO8601
does not produce a valid ISO8601 representation. DATE_ATOM does though –
Rialto How to convert from ISO 8601 to unixtimestamp :
strtotime('2012-01-18T11:45:00+01:00');
// Output : 1326883500
How to convert from unixtimestamp to ISO 8601 (timezone server) :
date_format(date_timestamp_set(new DateTime(), 1326883500), 'c');
// Output : 2012-01-18T11:45:00+01:00
How to convert from unixtimestamp to ISO 8601 (GMT) :
date_format(date_create('@'. 1326883500), 'c') . "\n";
// Output : 2012-01-18T10:45:00+00:00
How to convert from unixtimestamp to ISO 8601 (custom timezone) :
date_format(date_timestamp_set(new DateTime(), 1326883500)->setTimezone(new DateTimeZone('America/New_York')), 'c');
// Output : 2012-01-18T05:45:00-05:00
ISO 8601 is basically represented in PHP as "Y-m-d\TH:i:sP"
You can get this value from a constant:
DateTime::ATOM
- for PHP versions below 7.2 (was removed)
DateTimeInterface::ATOM
- for PHP versions since 7.2
DateTime::ATOM
has not been removed in PHP 7.2. –
Goodsell If you try set a value in datetime-local
date("Y-m-d\TH:i",strtotime('2010-12-30 23:21:46'));
//output : 2010-12-30T23:21
$datetime->format('Y-m-d\TH:i:s.u\Z')
should give the proper format, with the "T" separator, "Z" timezone (make sure to convert to UTC first) and microseconds (omit .u
if you don't intend to support fractional seconds).
See https://mcmap.net/q/161151/-in-an-iso-8601-date-is-the-t-character-mandatory for discussion why should use T
According to PHP offcial documentation you can simply format it to:
echo $objDateTime->format('c'); // ISO8601 formated datetime
echo $objDateTime->format(DateTime::ISO8601); // Another way to get an ISO8601 formatted string
You can try this way:
$datetime = new DateTime('2010-12-30 23:21:46');
echo $datetime->format(DATE_ATOM);
DATE_ATOM
and c
were already posted on this page years earlier. –
Frostwork To represent the date "2010-12-30 23:21:46" to the ISO 8601 format. Here's how you can do it:
<?php
$date = 2010-12-30 23:21:46;
$iso8601_date = date('c', strtotime($date)); // Convert to ISO 8601 format
?>
If you are getting date from your Database try:
<?php
$date = $row['date']; // Assuming $row['date'] contains "2010-12-30 23:21:46"
$iso8601_date = date('c', strtotime($date)); // Convert to ISO 8601 format
Call your Output
<?php echo($iso8601_date) ?>
You can also get your timestamps conversion via mutation inside modal like this
class YourModal extends Model
{
public function getCreatedAtAttribute($date)
{
return date(DATE_ISO8601, strtotime($date)); // ISO 8601 Date Format
}
}
Try this out
public static function toISO8601(string $dateString): string {
$dateTime = new \DateTime($dateString, new \DateTimeZone('UTC'));
return $dateTime->format('Y-m-d\\TH:i:s.vp');
}
Output will be: 2010-12-30T04:56:00.000Z
© 2022 - 2024 — McMap. All rights reserved.
date($format, strtotime($dateString))
or theDateTime
object equivalent. They only differ in the input to those functions. All you have to do is go to the correponding pages in the PHP Manual and find out what the input is. – Parrott