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'));
After PHP 5 you can use this: echo date("c"); form ISO 8601 formatted datetime.
http://ideone.com/nD7piL
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.
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
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
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
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->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://stackoverflow.com/a/9532375/65387 for discussion why should use T
You can try this way:
$datetime = new DateTime('2010-12-30 23:21:46');
echo $datetime->format(DATE_ATOM);
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
}
}
Related
I have my date in the following format:
01-02-2019
and I need to get it into the following format:
2019-02-01T00:00:00.000
Is it strtotime I need? I find that a bit confusing to use.
strtotime("01-02-2019");
Thank you for your help.
As #Vidal said, it's look like ISO 8601 but it's a bit different on your example.
If this is the exact result you need, here's how:
echo \DateTime::createFromFormat('d-m-Y','01-02-2019')->format('Y-m-d\TH:i:s.v');
will display : 2019-02-01T17:38:33.000
More about the format parameters: http://php.net/manual/en/function.date.php
Edit: I would recommend Vidal answer if this exact format is not mandatory since it's respecting an ISO norme.
I think the format you want is ISO 8601.
Here the php code.
<?php
$unixTimestamp = date("c", strtotime('01-02-2019'));
?>
As Vidal said in their answer, I think the ISO 8601 standard is what you're after.
I personally prefer the OOP approach, and would recommend always using PHP's DateTime() class over strtotime(), but either will do what you're looking for.
To do the same formatting with DateTime, simply instantiate the DateTime object, and format it as you would with strtotime() like so:
// Build a date object
$myDate = new DateTime('01-02-2019');
// Format and display in the ISO 8601 standard using
// the 'c' format option.
echo $date->format('c');
The magic here is in the 'c' format string, which represents the ISO 8601 constant available to the DateTime class.
More info on DateTimeInterface constants
You can do this way if you want to convert it to another format
<?php
// Create a new DateTime object
echo date('Y-m-d\TH:i:s.v',strtotime('01-02-2019'));
?>
OR
<?php
// Create a new DateTime object
$date = DateTime::createFromFormat('d-m-Y', '01-02-2019');
echo $date->format('Y-m-d\TH:i:s.v');
?>
DEMO1: https://3v4l.org/IlCur
DEMO2: https://3v4l.org/6CMKT
How do I convert my time from 2010-12-30 23:21:46 to ISO 8601 date format?
I tried $datetime->format(DateTime::ATOM) and date(DATE_ISO8601, strtotime('2010-12-30 23:21:46')) which converts into ISO8601 format like 2016-12-31T09:47:50+0000. But what i need is 2018-02-19T05:43:59.753000Z format. How can i achieve using PHP??
If you wish to output format in Zulu/UTC/GMT timezone, then you must first convert timezone, since I do not know in which timezone is your current setup.
$dt = new DateTime('2010-12-30 23:21:46');
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d\TH:i:s.u\Z');
demo
And ISO-8601 is a standard that has many variations of format:
2018-04-03
2018-04-03T05:20:03+00:00
2018-04-03T05:20:03Z
20180403T052003Z
2018-W14
2018-W14-2
...
This are all valid ISO-8601 formats. Read more here.
I am not sure how to convert timestamp into ISO-8601 and also apply UTC.
However, I am looking for following output:
2017-09-23T01:08:36.6437128Z ( Format = ISO-8601. Timezone = UTC)
I have tried couple of ways to get this but I am not getting it so anyone know how to get this. As this must be very quick and easy fix but I am missing something and that's the reason I am not getting this output.
Any help would be appreciated:)
Thanks
First of all, your desired format is not ISO 8601 compliant because you're including microseconds.
Use date_default_timezone_set to get UTC and date to easily format a timestamp.
Demo: https://3v4l.org/aRKqi
$timestamp = 1506086214;
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// ISO 8601 (does NOT include microseconds)
echo date("c", $timestamp) . PHP_EOL;
// custom format e.g. 2017-09-23T01:08:36.6437128Z
echo date("Y-m-d\TH:i:s.u\Z", $timestamp) . PHP_EOL;
2017-09-22T13:16:54+00:00
2017-09-22T13:16:54.000000Z
I want to convert this string '2016-05-30 15:35:00' into ISO 8601 format which should look like this:
Expected result : 2016-05-30T15:35:00+05:30
I tried following, but in the output it's 2 hours less,
$phpdate = strtotime('2016-05-30 15:35:00');
echo gmdate('Y-m-d\TH:i:s\+05:30', $phpdate);
Output : 2016-05-30T13:35:00+05:30
Have I made a mistake somewhere? or is there an easier way for this conversion?
You're using gmdate function of PHP which converts date into UTC format instead you need to use date function like as
date_default_timezone_set("Asia/Kolkata");//Set your timezone to Asia/Kolkata
$phpdate = strtotime('2016-05-30 15:35:00');
echo date('c', $phpdate);// 2016-05-30T13:35:00+05:30 if your timezone is of Asia/Kolkata
for an easier way to handle dates i recommend Carbon
for your problem it would be :
$date = new Carbon("2016-05-30 15:35:00");
echo $date->toIso8601String();
Paypal returns a timestamp of the following format:
yyyy-MM-ddTHH:mm:ssZ
And I don't quite know what to do with it...
How can I convert it to yyyy-MM-dd HH:mm:ss using my local timezone in php?
I'm tempted to preg_replace the mysterious letters, but something tells me there must a better way. There also appears to be 8 hours difference to my zone which I'm not sure how to substract.
Use DateTime class to do your magic.
$date = new DateTime('2012-09-09T21:24:34Z');
$date->format('Y-m-d'); # read format from date() function
You can use strtotime() to get a UNIX timestamp. From there you can do whatever you need: DateTime object, date(), etc.
Example with date():
echo date('r', strtotime('2012-09-10T10:00:00Z'));