API response format for date+time (iso thing?) - php

What's the standard (or some kind of iso-conform thing) format to display date (year, month, day) and time (Hours minutes and seconds) together as an result of an API Request? Or should a timestamp be returned in this case?

Use ISO8601 format. And use UTC timezone always. Its the standard by ISO and should be parsable by any library.
Example 2012-02-28T20:27:21+0000
In php use DateTimeZone and DateTime object combindly to get an ISO8601 date.
$dt = new DateTime("now", new DateTimeZone("UTC"));
echo $dt->format(DATE_ISO8601)`
See Date/Time in PHP manual to know more.

I think returning timestamp would be a better idea.
It allows your API users to manipulate it easily.
(My opinion)

Either do a integer unix timestamp or do full ISO8601.

RFC822 Format: 18 Feb 2012 14:27:18 -0000
DAY MONTH YEAR HOUR:MINUTES:SECONDS UTC-OFFSET
Using date():
date('d M y H:i:s O')
Twitter's API uses it, And some of its benefits are that it's readable by humans and easy to parse in PHP, JavaScript, And other languages. And you know which timezone the date is presented in.

Related

How can I change my date to this format?

I currently have a date formated like this:
2017-11-02 11:44:24
However; I need it in this format: 2014-03-11T14:49:52
This is due to the use of a RESTful API based on oData. How can I achieve this date format?
This looks like a job for: DateTime:createFromFormat
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2017-11-02 11:44:24');
echo $date->format('Y-m-d\TH:i:s');
Once you've got the DateTime object then you can format it as needed with ->format()
The date and time can be tricky, but what you want to be sure of is the date and time for your region or Greenwich Mean Time (GMT) or Universal Time Coordinate (UTC). Both GMT and UTC refer to the same time, but your server/database/publish date is likely in your local time. Here's an example of what you can use for local time:
date_default_timezone_set("America/New_York");
$date = date('Y-m-d\TG:i:s');
echo $date;
This will echo 2017-11-02T01:02:58. The capital G represents a 24 hour time stamp, and a small g will represent a 12 hour time stamp.
Depending on how your server is setup, without the date_default_timezone_set you will get the UTC time.

What is the difference between these two date?

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.

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.

Convert PHP datetime to MySQL RFC-822 valid Date-Time for RSS Feed

I want to add a date/time, created in PHP, to MySQL that is valid in a RSS Feed.
I'm using in PHP
$d = date( 'Y-m-d H:i:s T', time() );
$mysqldate = gmdate(DATE_RSS, strtotime($d));
inserting that into a DATETIME field in my database
But it saves it in this format Wed, 02 Oct 2002 08:00:00
and it need to be in this format to be RFC-822 valid Wed, 02 Oct 2002 08:00:00 EST
Use DATE_RFC822 instead of DATE_RSS
Why are you converting a php date time to a string, then converting that string back into a datetime object again? that's a serious waste of cpu cycles. why not simply do
$mysqldate = gmdate(DATE_RSS, time())?
as well, gmdate generates a UTC timestamp, for which there is no timezone - it's always GMT+0
Answer is very simple just use this format:
<pubDate>'.date('r', strtotime($rss_row['your_date_field'])).'</pubDate>
The original echo is:
echo date('r', strtotime($my_date));
This turns the datetime from inside our MySQL table which is formatted like: YYYY-MM-DD HH:MM:SS and turns it into a UNIX string that represents the same date.
Once we do that to our date, we have the date() function’s many wonders at our disposal. Going into those many wonders is more than I’ll get into here, but I’ve posted about it here.
In this particular case, adding the ‘r’ parameter to the date() function does all the work of reformatting our UNIX string into our RSS feed date format.
It’s that easy.
When saving something into a MySQL DATETIME field, you can not specify the format. DATETIMEs are stored in an internal format that is only concerned with the time value, not with the formatting. You will always have to format the date the way you need it after (or while) retrieving it from the database:
date(DATE_RSS, strtotime($dateFromDatabase));
To insert a date into the database you just need to provide it in a format MySQL understands, it will then be converted to said internal format and the formatting will be lost:
sprintf("INSERT INTO `foo` (`date`) VALUES('%s')", date('Y-m-d H:i:s'))
date('r', strtotime($object->pubDate))
-works nice
Actually DATETIME fields don't save any information about the timezone (http://dev.mysql.com/doc/refman/5.1/en/datetime.html).
So you need to append the timezone AFTER fetching the date from the database.

PHP: Is timezone data somehow embeded into Timestamps?

date_default_timezone_set("Asia/Singapore"); // UTC +8
$dt = new DateTime();
$dt->setTimestamp(DateTime::createFromFormat('u', gmdate('u'))->getTimestamp());
echo $dt->getTimezone()->getName(); // Asia/Singapore
echo $dt->format('d M Y H:i:s'); // Correct local time
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('d M Y H:i:s'); // Correct UTC Time
die;
I am wondering if timestamps contain timezone data. On line 3 you see that I used gmdate() which should give me UTC/GMT time. But when I get the timezone & formatted datetime, they are in localtime. I didn't set timezones in my DateTime object yet, gave it a UTC timestamp. It somehow knew to convert to localtime. Which makes me wonder if Timezone data are included in timestamps
setTimestamp accepts the timestamp in GMT 0 and you passed it in GMT, because of gmdate('u'). DateTime object takes your current timezone by default.
After that - you have properly set timestamp and current timezone, that is why DateTime object formats the date for Singapore.
Which makes me wonder if Timezone data are included in timestamps
No. Timestamp stores just amount of seconds since unix-epoch.
Timestamps are interpreted according to the interpreters timezone.
So no, they are standard and do not contain timezone data within themselves.
your example proves it, as you change the timezone, so does your result.
If the timezone data was embedded, the result would not change.

Categories