I've gone through all of the Moodle time functions, and I believe I have now seriously injured myself...
What I want to do is store a time of day (e.g. 1400) in a field corrected to GMT from the user's time. So the user enters the time of day, what function(s) do I call to correctly convert this to GMT before putting it in the data field?
mike
Help Storing Time Field
Number of replies: 9Re: Help Storing Time Field
The two main functions convert a user representation into a timestamp:
make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99, $applydst=true);
and back again
userdate($date, $format='', $timezone=99, $fixday = true)
A timestamp identifies a particular absolute second of time. There are no functions to convert a time-of-day only ... why do you need it? How does it work?
make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99, $applydst=true);
and back again
userdate($date, $format='', $timezone=99, $fixday = true)
A timestamp identifies a particular absolute second of time. There are no functions to convert a time-of-day only ... why do you need it? How does it work?
Re: Help Storing Time Field
I'm doing a mod that has a way of scheduling daily events by time. So the teacher has a table interface that allowd them to check the day of the week and then enter the time of day for that day of the week for the event to occur.
The field I need to store is not an absolute time, but a time of day. So, the teacher might enter 1400 (2:00 pm), and I need to convert this to the GMT equivalent before storing it.
mike
The field I need to store is not an absolute time, but a time of day. So, the teacher might enter 1400 (2:00 pm), and I need to convert this to the GMT equivalent before storing it.
mike
Re: Help Storing Time Field
The GMT time of that may vary at different times of the year! 
Don't you just wish DST wasn't invented?
One approach might be to store the first event as full timestamp using make_timestamp ... then at display time add (n * week-of-seconds) to it to bring it up to the correct week, then use userdate to make it display correctly for the current user.
Don't you just wish DST wasn't invented?
One approach might be to store the first event as full timestamp using make_timestamp ... then at display time add (n * week-of-seconds) to it to bring it up to the correct week, then use userdate to make it display correctly for the current user.
Re: Help Storing Time Field
That's why I'm glad I'm not trying to do the DST fix. Just following that thread has given me nose bleeds... 
I think what I'll do is make a timestamp for today with the time I'm interested in, and then subtract a timestamp of today at midnight.
something like:

I think what I'll do is make a timestamp for today with the time I'm interested in, and then subtract a timestamp of today at midnight.
something like:
$now = getdate();
$midnight = make_timestamp($now['year'], $now['month'], $now['day']);
$todaysched = make_timestamp($now['year'], $now['month'], $now['day'], $new_hour, $new_minute);
$schedtime = $todaysched - $midnight;
I think that will give me a time of day in GMT, adjusted by the user's time zone.
mike
Re: Help Storing Time Field
What you propose would work out fine as far as I can see, except if today is the day when DST changes!
For example, on any normal day the result of the subtraction would be something like 14 * 3600, meaning 14:00 for that day. However, if that day the DST changes at say 02:00, then the result of the subtraction would be 13 * 3600 or 15 * 3600, but not 14.
This may be nitpicking, but still...
I think the way to do it would be to store the hour by itself (e.g. integer 14), and when you need to convert to a timestamp for a specific date use make_timestamp(). That's the way that I treat computing the timestamps when the DST will change (I know the day and the hour, I need the timestamp).
Also, I don't know if it helps, but in Moodle 1.5 you can use dst_offset_on($timestamp) which will return you the amount of offset that the current user was/is/will be experiencing that moment when compared to a non-DST-aware user in the same timezone.
For example, on any normal day the result of the subtraction would be something like 14 * 3600, meaning 14:00 for that day. However, if that day the DST changes at say 02:00, then the result of the subtraction would be 13 * 3600 or 15 * 3600, but not 14.
This may be nitpicking, but still...
I think the way to do it would be to store the hour by itself (e.g. integer 14), and when you need to convert to a timestamp for a specific date use make_timestamp(). That's the way that I treat computing the timestamps when the DST will change (I know the day and the hour, I need the timestamp).
Also, I don't know if it helps, but in Moodle 1.5 you can use dst_offset_on($timestamp) which will return you the amount of offset that the current user was/is/will be experiencing that moment when compared to a non-DST-aware user in the same timezone.
Re: Help Storing Time Field
I can't just store the 14 though, because that won't be GMT. When the instructor enters the clock time for the schedule (e.g. 1400), I need to adjust it to GMT based on the instructor's timezone. Otherwise, I don't really know what time is meant by 1400.
If I waited until I needed it, I would have to assume that the time entered was GMT, and it may not be the real time.
I think.
mike
If I waited until I needed it, I would have to assume that the time entered was GMT, and it may not be the real time.
I think.
mike
Re: Help Storing Time Field
OK then, do get_user_timezone() at the time the instructor enters the hour, and subtract that from the hour to get the GMT hour. Could that be it?
Re: Help Storing Time Field
Yes. I believe you're right. I didn't realize it was that simple. For some reason, I had it in my head that the user timezone was a code and didn't realize it was stored as an hour offset.
Now, just to doublecheck, wouldn't I have to add the timezone to the specified time to get GMT? The user will have entered a time in their timezone. To get back to GMT, don't I have to add?
mike
Now, just to doublecheck, wouldn't I have to add the timezone to the specified time to get GMT? The user will have entered a time in their timezone. To get back to GMT, don't I have to add?
mike
Re: Help Storing Time Field
No, you have to subtract. For example, in Greece it's GMT+2, so you have to subtract 2 from localtime to get GMT. And subtracting negative timezones amounts to adding, which is of course correct.
Edit: you might get difficulties though in the case where the GMT hour actually falls out of bounds for the day due to the instructor's timezone. Continuing the Greece example, if I set the hour to be 01:00, then subtracting 2 to get GMT would put the event at 23:00 the previous day. You need to find a way to handle that. Same goes for negative instructor timezones, where the event could be pushed into the next day.
Edit: you might get difficulties though in the case where the GMT hour actually falls out of bounds for the day due to the instructor's timezone. Continuing the Greece example, if I set the hour to be 01:00, then subtracting 2 to get GMT would put the event at 23:00 the previous day. You need to find a way to handle that. Same goes for negative instructor timezones, where the event could be pushed into the next day.