What is the difference between these two date? - php

I have two date:
2014-12-01T12:05:59Z //dont know which format
And
2014-12-01T03:59:00-08:00 //i'll get it from date("c") function in php (ISO 8601).
So, I don't know what is the different in this date's?
And how to convert date in 2014-12-01T12:05:59Z this formate in php?
[UPDATE]
I want to get current timestamps in in 2014-12-01T12:05:59Z this date formate.

It's the ISO 8601 format and both time zone variants are allowed and must be expected. -08:00 means 8 hours behind UTC, Z means UTC and is a shortcut for +00:00.

Both the dates you have mentioned are dates with timezone. The only difference is that the first date has 'z' at the end which is a Zone Designator for UTC and the later date is suffixed with timezone offset, which is generally represented as "HH:MM". Practically, both dates are same and both the representations are correct.
The 'z' suffixed representation is generally accepted xml standard for timestamp in API payloads.
Looking at your requirements, since your API provider needs the date with Zone Designator, they must be calculating time in UTC.
I will suggest changing your timezone to UTC first using
date_default_timezone_set('UTC');
and then use this expression to get required timestamp
date('Y-m-d\TH:i:s\Z', time());
This way you are saved from the timezone conflict and you will also be able to send the required date to the API.

date_default_timezone_set('UTC');
echo gmdate('Y-m-d\TH:i:s\Z');
Check the link. You will get the difference as well.

Related

OpenWeatherMap API - Dates in XML

Anyone can explain to me in which format are these DateTime strings from OpenWeatherMap API and how to work with them in order to:
Just see the time (Greenwich +1)
sun rise="2019-01-28T06:31:03" set="2019-01-28T16:14:39"
View the Date & Time (Greenwich +1)
lastupdate value="2019-01-28T11:20:00"
Those dates are in ISO 8601
The examples you actually have do NOT contain any TimeZone information so they could be any timezone..
If you work with datetime offsets (that include time zone information) you would typically see something like this
2008-09-15T15:53:00+05:00
The reasone it is formatted in ISO 8601 is so that the date time picker (that is the actual format you use for the value) on your HTML page knows how to interpret the time into your local (machine clock configured time)
Typically if using UTC you will have a date time that looks like
2008-09-15T15:53:00Z
Which is Zulu Time
And as i mentioned already if it does not have any of those it is assumed local time at server that generated it.
Thanks, I was able to convert them by using concatenated date and strtotime functions.
date('H.i',strtotime($sunset_time));
The local time on the server is Greenwich, whereas I would need to add +1 for my time zone (Europe/Rome).
In the end I was able to add +1 hour like this:
date('H.i', strtotime($sunset_time) + 60*60);
Is this a correct way?

What is the right ISO8601 format?

Why
echo date('c');
not equal with
$datetime = new DateTime();
echo $datetime->format(DateTime::ISO8601);
The result:
2016-07-07T21:18:22+03:00
2016-07-07T21:18:22+0300
Both must give current time in ISO8601 format. In Wikipedia right format is 2016-07-07T21:18:22+03:00 but some banks use 2016-07-07T21:18:22+0300 format in API. Why?
2016-07-07T21:18:22+03:00
Is the correct ISO 8601:2004 representation.
2016-07-07T21:18:22+0300
Is incorrect, the zone designator may not be in the basic format when the date and time of day is in the extended format.
ISO 8601:2004 4.3 Date and time of day:
[...] the expression shall either be completely in basic format, in
which case the minimum number of separators necessary for the required
expression is used, or completely in extended format, in which case
additional separators shall be used [...]
Update 1:
ISO 8601 specifies three differerent date representations: calendar, ordinal and week dates. Theese can be formatted in either basic format (minimum number of seperators) or extended format (extension of the basic format that includes additional separators). ISO 8601 requires that the resulting expression is either consistently in basic format or consistently in extended format.
Combination of calendar date and time of day in local time with difference from UTC:
2016-07-07T21:18:22+03:00 (extended format)
20160707T211822+0300 (basic format)
Combination of ordinal date and time of day in local time with difference from UTC:
2016-189T21:18:22+03:00 (extended format)
2016189T211822+0300 (basic format)
Combination of week date and time of day in local time with difference from UTC:
2016-W27-4T21:18:22+03:00 (extended format)
2016W274T211822+0300 (basic format)
All of the above representations represent the same date and time of day in local time with difference from UTC (and instant). If an API documents that it accepts an ISO 8601 date with time of day and a zone designator (aka known as a complete representation), it should accept all of the above representations to be compliant with ISO 8601.
Update 2:
Most bugs i have encountered stem from using strftime() to output an ISO 8601 date and time of day in local time with difference from UTC in extended format. The standard strftime() can only output compliant representations in basic format due to the limitations in the z conversion specifier:
Combination of calendar date and time of day in local time with difference from UTC:
Format: Example:
%Y%m%dT%H%M%S%z 20160707T211822+0300
Combination of ordinal date and time of day in local time with difference from UTC:
Basic format: Example:
%Y%jT%H%M%S%z 2016189T211822+0300
Combination of week date and time of day in local time with difference from UTC:
Basic format: Example:
%GW%V%uT%H%M%S%z 2016W274T211822+0300
The GNU strftime implementation supports a : flag between the percent and the z conversion specifier to specify that the zone designator should be formated in extended format:
Combination of calendar date and time of day in local time with difference from UTC:
Format: Example:
%Y-%m-%dT%H:%M:%S%:z 2016-07-07T21:18:22+03:00
Combination of ordinal date and time of day in local time with difference from UTC:
Format: Example:
%Y-%jT%H:%M:%S%:z 2016-189T21:18:22+03:00
Combination of week date and time of day in local time with difference from UTC:
Format: Example:
%G-W%V-%uT%H:%M:%S%:z 2016-W27-4T21:18:22+03:00
Still PHP does not interpret ISO8601 correctly when milliseconds are given, even with DateTime::ATOM.
$d=DateTime::createFromFormat(DateTime::ATOM,"2018-01-10T01:00:00.000Z");
// null
or using Carbon:
echo Carbon\Carbon::createFromFormat(Carbon\Carbon::ATOM,"2018-01-10T01:00:00.000Z","UTC");
// InvalidArgumentException with message 'The timezone could not be found in the database'
The best way is to let Carbon or datetime figure it out itself:
$d = new Carbon\Carbon("2018-01-10T01:00:00.000Z");
// -> 2018-01-10 01:00:00
$d = new Carbon\Carbon("2018-01-10T01:00:00Z");
// -> 2018-01-10 01:00:00

How to make conversion from string to timestamp one-to-one?

I'm totally confused with PHP UTC/Local time conversions. Some functions convert time implicitly, some don't. Argh!!!
Say, I have this PHP code:
$timestamp = strtotime("2016-05-13 09:26:30"); //From MySQL date format
//$timestamp returned is 1463153190
Then if I convert 1463153190 to UTC time, it becomes 05/13/2016 # 3:26pm (UTC). Why???
How do I get a function to convert time from string to a timestamp one-to-one, without any time-zone shenanigans?
2016-05-13 09:26:30 is an incomplete timestamp, since it's missing the timezone. It could refer to over 24 different "absolute points in time", depending on which timezone it's being interpreted in. 1463153190 is an absolute UNIX timestamp for an absolute point in time. To convert your former timestamp to an absolute point in time, it needs to be interpreted as some timezone. The conversion the other way around also needs some timezone to format the point in time in.
In PHP the default timezone is considered when converting to or from UNIX timestamps. You can set that using date_default_timezone_set.
You see what you're seeing because date_default_timezone is set to some timezone (~+6) when converting from the human readable format to the UNIX timestamp, but then you're converting it back explicitly using UTC. Your initial timezone was simply not UTC/the timestamp was not initially interpreted as being in UTC. There's a timezone mismatch between the initial value and the final value, hence an offset of 6 hours.

Get openssl certificate expiration date

I've parsed openssl certificate by openssl_x509_parse() function and got an array as a result.
Now I need to get the expiration time of that certificate. In parsed array I have validTo_time_t element which contains a valid unix timestamp. But how to determine what timezone this timestamp belongs for?
So I can't get the real expiration time because that timestamp because it means deifferent dates on different timezones.
php formats this field using it's default timezone. you can get it using http://docs.php.net/date_default_timezone_get function
and once you know the timezone you can convert it to UTC or whatever you need
Unix TimeStamp have no timezone.
It's defined as number of seconds since January 1st, 1970 at UTC.
It's not a defined as a real date but just a bunch of seconds.
No timezone = no problems.
This is why it's a perfect measure of time across different server and different regions. You can just store unix timestamp, and when you convert it to a date it will use your local timezone to determinate the date, this is completly automatic and this means that when you convert givin a different timezone it will convert to the correct date. Always.
This is true for all timestamp not just for the SSL output.
Major info on Timestamp http://www.unixtimestamp.com/

What is the time format used in facebook created date?

Hi i am working on facebook Graph API where i need all the posts information of a group. So I did it and saw [created_date'] => '2013-01-25T00:11:02+0000' what does this date and time represent i mean i know 2013-01-25 is date and 00:11:02 is time but what does T and +0000 represent.
BTW where is the server of facebook. Which timestamp should i use to match facebook time?
Thank you.
T = TIME and the +0000 is timezone offset. Facebook uses localized timezones. You can request a Unix timestamp instead of the string by adding the parameter: date_format=U to your graph API call.
Please see this link for more information.
The date format is called ISO 8601. The letter T is used to separate date and time unambiguously and +0000 is used to signify the timezone offset, in this case GMT or UTC.
That said, you generally don't need to worry so much about the actual contents; rather you should know how to work with them. To use such a date, you can use strtotime() to convert it into a time-stamp:
$ts = strtotime('2013-01-25T00:11:02+0000');
To convert the time-stamp back into a string representation, you can simply use gmdate() with the predefined date constant DATE_ISO8601:
echo gmdate(DATE_ISO8601, $ts);
Alternatively, using DateTime:
// import date
$d = DateTime::createFromFormat(DateTime::ISO8601, '2013-01-25T00:11:02+0000');
// export date
echo $dd->format(DateTime::ISO8601), PHP_EOL;
This is a standard format, specifically ISO 8601.
As much as I don't like linking to it, http://www.w3schools.com/schema/schema_dtypes_date.asp does have a good "human-understandable" explanation:
The dateTime is specified in the following form "YYYY-MM-DDThh:mm:ss"
where:
YYYY indicates the year
MM indicates the month
DD indicates the day
T indicates the start of the required time section
hh indicates the hour
mm indicates the minute
ss indicates the second
To specify a time zone, you can either enter a dateTime in UTC time by
adding a "Z" behind the time - like this:
2002-05-30T09:30:10Z
or you can specify an offset from the UTC time by adding a positive or
negative time behind the time - like this:
2002-05-30T09:30:10-06:00
or
2002-05-30T09:30:10+06:00
Therefore, in your case the +0000 indicates a time offset of 0 from UTC.

Categories