outlook.com api event creation date - php

I am working with PHP and Outlook REST API, I am able to create the events just fine except that the date I give API to create events is getting incremented by 11 hours and something is wrong with end_date also, for example if i am telling the API to create the event from 2014-03-31T15:00:00-0000 to 2014-03-31T16:00:00-0000 it ends up creating the end from 2014-04-01T02:00:00+1100 to 2014-04-01T02:30:00+1100
This is what my API URL looks like
https://apis.live.net/v5.0/9898ef90931244e8/events?name=testing_event&description=event_description&start_time=2014-03-31T15:00:00-0000&end_time=2014-03-31T16:00:00-0000&access_token=token-goes-here&method=POST
if I remove the -0000 at the end, i get the following error
{
"error": {
"code": "request_parameter_invalid",
"message": "The value of input parameter 'start_time' isn't valid. The expected formats for this parameter are the following: '1970-01-01T00:00:00Z', '1970-01-01T00:00:00.000Z', '1970-01-01 00:00:00Z'. In all cases, 'Z' is interchangeable with a time zone offset of the form: '+00:00', '-00:00', '+0000' or '-0000'."
}
}
I will really appreciate any help as I have spent hourssss trying to fix this and I am pretty much out of all ideas..

Try using Z for local time like this:
2014-03-31T15:00:00.000Z
So it would be something like
https://apis.live.net/v5.0/9898ef90931244e8/events? name=testing_event&description=event_description&start_time=2014-03-31T15:00:00Z&event_start_time=2014-03-31T16:00:00Z&access_token=token-goes-here&method=POST

It's the same time with a different time zone (the -0000 and +1100).

A while ago I posted this question at MSDN Forum and forgot about it, today i visited the question and this is the reply I got from Outlook team explaining how to understand UTC timezone, I hope this helps someone out there stuck at the same problem and I wish they included this in their documentation. This fixed the problem I was having
*The ISO 8601 format for dates can be a bit confusing. What you're seeing in your response is actually correct. The calendar service
takes your date and maps it to the time zone of the user.
"2014-03-31T15:00:00-0000" means 3 PM on March 31, 2014, with an
adjustment of 0 hours and 0 minutes to translate to UTC time. In other
words, this basically IS UTC time. The calendar service then
translates this into the local time zone of the user for whom you are
creating the event. "2014-04-01T02:00:00+1100" means 2 AM on April 1,
2014, for whatever time zone your user is in. To translate back to
UTC, you need to SUBTRACT the offset from the local time. So 2 AM,
April 1, 2014 in your time maps to 11 hours earlier UTC, which is 3 PM
March 31, 2014 (what you entered).
If you're using Z, that means that you are essentially specifying a
UTC time. So to get the desired time for your event you need to take
your desired time and figure out what the corresponding UTC time is.
So if the UTC offset of your user is +1100 you would need to subtract
that offset from the local time of your event to calculate the UTC
time. In your case I believe this would be 2014-03-31T04:00:00Z. This
is actually something that's best left to code instead of trying to
figure out how to do yourself. If you take a look at the "Creating
Calendar Events" sample at http://isdk.dev.live.com it shows you how
to do this in JavaScript - maybe there's a PHP equivalent.*

Related

Some questions about EDT and time difference

Introduction to my website
My website is for visitors in Korea(AKA Republic of Korea).
And the server for My website is in the United States of America.
And PHPMyAdmin displays EDT when it runs a query SELECT ## system_time_zone.
Structure of my website
When I first uploaded my website to this server in October this year, I checked the DB time.
And it seemed that there was a time difference of 13 hours with Korea. So I added 3600 * 13 seconds to DB time(without setting timezone) as follows.
const Offset = 3600 * 13;
$SelectNow = $PDO->prepare('SELECT DATE_ADD(NOW(), INTERVAL '.Offset.' SECOND)');
$SelectNow->execute() or exit;
$DbNow = $SelectNow->fetchColumn();
My website takes $DbNow as above and uses it in various situations.
For example, in the posting situation, enter $DbNow in the datetime field of the INSERT INTO query as follows:
$WriteNote = $PDO->prepare('INSERT INTO table_note(my_datetime, my_contents) VALUES("'.$DbNow.'", :my_contents)');
$WriteNote->bindValue(':my_contents', $my_contents, PDO::PARAM_STR);
$WriteNote->execute();
The problem situation
One day in November of this year, when I wrote a post and checked the date field(my_datetime) of the post, I got an additional time difference of one hour with Korea.
Apparently, at the end of October, I corrected the time difference of 3600 * 13. And then I confirmed that it matches the Korean time. However, in November, There is a time difference of one hour!
Guess the cause
It seems that US summer time is being applied to the DB server of my website. Did I guess right?
My question
1) How can I solve this time difference fundamentally?
Is it correct to convert DB time to KST?
Or is it the correct way to convert to UTC and then added 3600 * x to UTC?
2) Even though the problem is resolved, some of the existing data in my DB has a time difference of one hour with Korean time.
What query do I use if I want to select the data with a time difference?
And how much more or subtract it from the data to get rid of the 1 hour time difference?
Use UTC to store time in Database.
change your queries to insert with UTC datetimes.
Use external libraries to convert UTC to respective timezones.
(below are the my personal recommendation.)
There may be best of it.
PHP : Carbon
Javascript : Moment, moment timezone.
No, it takes timezone of Database server resides in.
little manual verification, or create a job to change all dates in UTC.
Edit:
http://carbon.nesbot.com/docs/
I mean you can create a script and run with cron job.

How to deal with timezones between server and client?

I'm developing a website where I have to deal with different possible timezones from the users. This becomes a great problem since the website hosts time-delicate events like auctions.
All dates/times on the server are in UTC. Database stores everything in UTC timestamps. PHP default timezone is set to UTC too (date_default_timezone_set('UTC');).
Now, my problem is how I should interact with the users, whether I'm only showing a date or, more important, I'm reading a date/time from user input.
A concrete example:
An auction has a deadline, which I store in database as UTC.
When I view the auction on the website, a javascript timer uses a Date object to calculate the remaining time. It automatically converts the timezone to GMT+0100 (my local timezone). So if the deadline is '2013-08-08 10:46:08' (UTC), the javascript date object will return Aug 08 2013 11:26:15 GMT+0100 (GMT Standard Time).
If the current time is greater than 11:46:08 then the timer says that the remaining time is 00:00 (which is correct).
But if I try to insert a bid, the server accepts since the condition on the MySQL INSERT evaluates to true:
INSERT INTO Bids ... WHERE ... AND auction_deadline > NOW() ...
( because auction_deadline = '2013-08-08 10:46:08' and NOW() = '2013-08-08 10:26:50')
All this mumbo jumbo of timezone melts my brains. What am I missing here? I'm almost certain that storing all dates/times in UTC inside the database is the best. I just can't think crystal clear how do deal with it between the user and the database.
Your problem doesn't involve timezones at all, just the fact that clients can turn their clocks or have their clock skewed considerably. For that the fix is to poll the server every once in a while for an offset fix to use in calculations.
In fact, you don't even need date objects. There is a certain universal instant in time when the auction ends. Let's say it is 1375960662823. Right now, the universal instant in time is 1375960669199, so from that we see that the auction ends in 6 seconds (1375960662823 - 1375960669199 ~ 6000 ). It will end in 6 seconds regardless if I am in Morocco or Japan. Do you understand it yet?
To generate these numbers, on the client side you can call var now = Date.now() + skewFix where skewFix is the correction that needs to applied in case client has time skew or manually set their computer to wrong time.
In PHP, you can generate it with $now = time() * 1000;
This is rather a typical subject yet very complex for most to understand. First thing, you never mention the DAYLIGHT SAVING. yeah I am increasing your tension :).
Now let us see how we can do this. You did a good job by saving the Time in UTC. Now, I hope you have registered members and that each member has ability to set their preferred timezone, otherwise you will show Server' timezone based time to them.
When you through "start time" to user you must send them after converting UTC time to their time, similarly when you accept TIME from browser be it user action or javascript you need to convert that time to UTC considering the fact that user is that time zone that he select for his profile.
Hope that clear the idea on where you are going wrong? Please read through day light saving as that will play an important role too when you move ahead with other logic on same.
EDIT:
You can use javascript's Timezone offset, for auto submission and user input based on his settings.
Date in JavaScript uses local timezone. You should get UTC time for the user and send it to the server
new Date
Thu Aug 08 2013 17:00:14 GMT+0530 (India Standard Time)
(new Date("Thu Aug 08 2013 17:00:14")).toUTCString();
"Thu, 08 Aug 2013 11:30:14 GMT"
This will resolve the timezone issue between the server and client.
You said
( because auction_deadline = '2013-08-08 10:46:08' and NOW() = '2013-08-08 10:26:50')
In MySQL - NOW returns the current time in the server's local time zone (docs).
You probably want something like UTC_TIMESTAMP which returns the current time in UTC (docs).
Also - you probably shouldn't accept any input time from the client JavaScript at all. Only trust your own clock. When a bid is placed, use the time on your server in MySQL or in PHP. Don't accept it as input.
You can do the following
once page is loaded, send an ajax request to server with timezone offset of user. You can get timezone offset using the following code.
var curdate = new Date()
var offset = curdate.getTimezoneOffset()
offset is timezone offset in minute.
I think it will help.
everytime when you get the date from the clientside, you can use the getUTC to convert to UTC date ie:
var todayDate = new Date();
var todayDateInUTC = new Date(todayDate.getUTCFullYear(), todayDate.getUTCMonth(), todayDate.getUTCDate(), todayDate.getUTCHours(), todayDate.getUTCMinutes(), todayDate.getUTCSeconds());
so right before you insert the bid date to database, use the getUTC functions to convert it into UTC format.

Using PHP to parse a Google Calendar XML - End date is off by a day

I am trying to make a webpage and have the next three events on a Google Calendar show up on the home page. I have been using this PHP (http://james.cridland.net/code/google-calendar.html) to access my XML feed and format it into HTML.
The problem I'm having is that for some reason a new day starts at 11am. For example if my Google Calendar has an event from 10am on the 20th of December that lasts an hour, my PHP output will show an event that starts at 10am on the 20th which ends at 11am on the 21st. Otherwise it is working fine.
I have set my time to local (New Zealand) time on my Google Calendar account, and in PHP using date_default_timezone_set("Pacific/Auckland");
The horrible line that calculates the finish date is
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+date("Z",strtotime($ns_gd->when->attributes()->endTime)));
where $dateformat is a string with the date format.
The Google Calendar XML gives a start and finish time of
2011-12-22T10:00:00.000+13:00
2011-12-23T11:00:00.000+13:00
respectively, and the PHP is calculating a timeframe of 10.00am 22 December 2011 to 2.00pm 23 December 2011.
Whats going on?!?!
This line is indeed horrible:
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+
date("Z",strtotime($ns_gd->when->attributes()->endTime)));
strtotime can handle this type of ISO 8601 dates just fine. This code-fragment is probably written under the assumption that strtotime dismisses the timezone and returns the datetime in UTC and therefore the timezone "correction" needs to be calculated manually - that's what the +date("Z", ...) stands for (with "Z" the second parameter - the timestamp - is actually ignored).
So in your example 13 hours are added to your dates. And 10:00 + 13:00 = 23:00 (11 pm) which is still on the same day, but 11:00 + 13:00 = 24:00 (12 am) which is actually 00:00 on a new day.
So the correct way to convert the date is:
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime));
Try the zend framework for google calendar(It worked for me better than reinventing the wheel): http://framework.zend.com/manual/en/zend.gdata.calendar.html (look at the examples, they're quite easy and helpful)

How to set time zone in PHP

I have a site that has list of stores in different countries ( different time zones ), and it should display when store is OPEN or CLOSED by working hours.
I have javascript that gets DEFAULT timezone GMT offset when daylight saving isn't set, put it inside mysql, and that part works correctly.
My timezone is GMT+1 ( and now it's daylight saving active, so it's +1 hour now )
I use php to change zone using this:
date_default_timezone_set('Etc/GMT+1');
echo date('h');
Time here: 10 PM
GMT time: 8 PM
And this code return: 7 PM
So it's like it instead increasing by 1, it decrease, and plus there is no daylight saving...
Can someone tell me what happen here?
Is it php bug or something wrong on server?
Thank you...
I'm currently chuckling because I recently had a similar problem. Unfortunately Etc/GMT timezones are deprecated. They break. My personal recommendation? Just use HH:MM or set it by the city (cities don't require daylight savings time adjustments!)
Perhaps date('I') might help in this situation.
Unless there is an option to define a look-up list [GMT offset] - [time zone name]. Then it will be possible to adjust time zone using DateTime\DateTimeZone classes.

Twitter timezone & created_at (utc time) values to calculate what time zone the user is in

I'm trying to estimate a twitter user's location based on the time_zone or created_at value in a given tweet object. However it seems that all created_at values I've come across are just in a pointless localised time and the time zone they supply isn't in the most helpful format
example tweet
"created_at":"Thu Jun 02 14:41:24 +0000 2011"
"time_zone":null
OR
"created_at":"Sun Jan 09 05:03:52 +0000 2011"
"time_zone":"Mountain Time (US & Canada)"
Both of these times aren't in UTC time and the time_zone isn't in any particular standard format with the rest of the world
I'm probably coming across really annoying, I'm tired and hot and been looking for this solution for too long :|
thanks,
Andy
This answer details how to convert a Twitter date into something PHP can work with; synopsis below.
strtotime("dateString"); gets it into
the native PHP date format, then you
can work with the date() function to
get it printed out how you'd like it.
As for the time zone, you can use the value of the utc_offset property of the user object to calculate it. For example, my profile's utc_offset is:
"utc_offset":-21600
Just divide that value by 3600 and that will give you -6, which is the US Central time zone. These will be absolute offsets and will not change whether the time zone honors daylight savings time.

Categories