Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #2010 > unrolled thread
| Started by | bill <nobody@spamcop.net> |
|---|---|
| First post | 2011-06-06 10:47 -0400 |
| Last post | 2011-06-07 06:45 -0400 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.php
determining the difference between two dateTimes bill <nobody@spamcop.net> - 2011-06-06 10:47 -0400
Re: determining the difference between two dateTimes The Natural Philosopher <tnp@invalid.invalid> - 2011-06-06 17:00 +0100
Re: determining the difference between two dateTimes Jerry Stuckle <jstucklex@attglobal.net> - 2011-06-06 13:58 -0400
Re: determining the difference between two dateTimes bill <nobody@spamcop.net> - 2011-06-07 06:45 -0400
| From | bill <nobody@spamcop.net> |
|---|---|
| Date | 2011-06-06 10:47 -0400 |
| Subject | determining the difference between two dateTimes |
| Message-ID | <cvednXbNbb7rdXHQnZ2dnUVZ_i2dnZ2d@cablespeedmi.com> |
In a scheduling program I need to check to see if there is a
conflict between an existing appointment and one that is to be
entered. I figured that I would use dateTime::diff
the existing appointment is returned as $row['apptTime'], the
appt being tested is $dateTM
$TAptEnd = $dateTM->add(new DateInterval('PT' . $duration .'M'));
$date = new DateTime($row['apptTime']); //dateTime object
of the beginning of the next appt
$diff = $date->diff($TAptEnd,true); //difference between
the beginning of an existing appt and the end of
the testing.
if ($diff->format('%i') <0 ) return 1;
I am running into several problems that suggest that this is not
the best way to go about this.
1: if the next appointment is the next day I am getting only the
time difference between the appointments, eg 10 min, not the
whole time. I would expect the difference to be 24 * 60 + 10,
not just the 10.
2: if I need to use the day + hours + minutes it gets really messy.
Suggestions please.
bill
[toc] | [next] | [standalone]
| From | The Natural Philosopher <tnp@invalid.invalid> |
|---|---|
| Date | 2011-06-06 17:00 +0100 |
| Message-ID | <isitih$996$1@news.albasani.net> |
| In reply to | #2010 |
bill wrote:
> In a scheduling program I need to check to see if there is a conflict
> between an existing appointment and one that is to be entered. I
> figured that I would use dateTime::diff
>
> the existing appointment is returned as $row['apptTime'], the appt being
> tested is $dateTM
>
> $TAptEnd = $dateTM->add(new DateInterval('PT' . $duration .'M'));
>
> $date = new DateTime($row['apptTime']); //dateTime object
> of the beginning of the next appt
> $diff = $date->diff($TAptEnd,true); //difference between
> the beginning of an existing appt and the end of
> the testing.
>
> if ($diff->format('%i') <0 ) return 1;
>
> I am running into several problems that suggest that this is not the
> best way to go about this.
>
> 1: if the next appointment is the next day I am getting only the time
> difference between the appointments, eg 10 min, not the whole time. I
> would expect the difference to be 24 * 60 + 10, not just the 10.
>
> 2: if I need to use the day + hours + minutes it gets really messy.
>
> Suggestions please.
>
> bill
Convert the whole lot using appropriate calls to remove all daylight
savings time, into seconds since Jan 1 1970..i.e.. UNIXTIME.
That will give you absolute offset in seconds.
If you are pulling the date times out of MySQL, the call
"set time_zone= '+00:00'"
means that any dates returned for the rest of the session will be UTC.
The the mysql function unix_timestamp(timestamp) will return the number
of seconds since Jan 1 1970.
If OTOH you are using some other means than an SQL database to store
times, I suggest you scrap them and use that particular way of
expressing times and dates. Its very easy to convert from that to the
local date in any form you want, using a variety of available functions
in PHP and or C or Mysql.. and you have a simple way of calculating date
offsets by subtracting two times, and divinding them by 60 - integer or
floating point - to get the number of minutes apart.
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2011-06-06 13:58 -0400 |
| Message-ID | <isj4h0$820$1@dont-email.me> |
| In reply to | #2010 |
On 6/6/2011 10:47 AM, bill wrote:
> In a scheduling program I need to check to see if there is a conflict
> between an existing appointment and one that is to be entered. I figured
> that I would use dateTime::diff
>
> the existing appointment is returned as $row['apptTime'], the appt being
> tested is $dateTM
>
> $TAptEnd = $dateTM->add(new DateInterval('PT' . $duration .'M'));
>
> $date = new DateTime($row['apptTime']); //dateTime object
> of the beginning of the next appt
> $diff = $date->diff($TAptEnd,true); //difference between
> the beginning of an existing appt and the end of
> the testing.
>
> if ($diff->format('%i') <0 ) return 1;
>
> I am running into several problems that suggest that this is not the
> best way to go about this.
>
> 1: if the next appointment is the next day I am getting only the time
> difference between the appointments, eg 10 min, not the whole time. I
> would expect the difference to be 24 * 60 + 10, not just the 10.
>
> 2: if I need to use the day + hours + minutes it gets really messy.
>
> Suggestions please.
>
> bill
Bill,
As you found out, '%i' just gives you the minutes portion of the DateTime.
What if you converted both DateTime objects to timestamps with
getTimestamp()? This will give you the Unix time (seconds since
1/1/1970) and you can subtract those to get the difference. Then a
simple comparison of the seconds will get you the answer you need.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | bill <nobody@spamcop.net> |
|---|---|
| Date | 2011-06-07 06:45 -0400 |
| Message-ID | <gOadnWbR3dXdnHPQnZ2dnUVZ_qmdnZ2d@cablespeedmi.com> |
| In reply to | #2013 |
On 6/6/2011 1:58 PM, Jerry Stuckle wrote:
> On 6/6/2011 10:47 AM, bill wrote:
>> In a scheduling program I need to check to see if there is a
>> conflict
>> between an existing appointment and one that is to be entered.
>> I figured
>> that I would use dateTime::diff
>>
>> the existing appointment is returned as $row['apptTime'], the
>> appt being
>> tested is $dateTM
>>
>> $TAptEnd = $dateTM->add(new DateInterval('PT' . $duration .'M'));
>>
>> $date = new DateTime($row['apptTime']); //dateTime object
>> of the beginning of the next appt
>> $diff = $date->diff($TAptEnd,true); //difference between
>> the beginning of an existing appt and the end of
>> the testing.
>>
>> if ($diff->format('%i') <0 ) return 1;
>>
>> I am running into several problems that suggest that this is
>> not the
>> best way to go about this.
>>
>> 1: if the next appointment is the next day I am getting only
>> the time
>> difference between the appointments, eg 10 min, not the whole
>> time. I
>> would expect the difference to be 24 * 60 + 10, not just the 10.
>>
>> 2: if I need to use the day + hours + minutes it gets really
>> messy.
>>
>> Suggestions please.
>>
>> bill
>
> Bill,
>
> As you found out, '%i' just gives you the minutes portion of the
> DateTime.
>
> What if you converted both DateTime objects to timestamps with
> getTimestamp()? This will give you the Unix time (seconds since
> 1/1/1970) and you can subtract those to get the difference. Then
> a simple comparison of the seconds will get you the answer you need.
>
Thanks guys for: 1) the good advice, and
2) not getting into a fight over it.
bill
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web