Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3258 > unrolled thread

comparing dates?

Started byMichael Joel <no@please.com>
First post2011-10-02 09:00 -0400
Last post2011-10-06 11:00 +0200
Articles 11 — 8 participants

Back to article view | Back to comp.lang.php


Contents

  comparing dates? Michael Joel <no@please.com> - 2011-10-02 09:00 -0400
    Re: comparing dates? houghi <houghi@houghi.org.invalid> - 2011-10-02 15:43 +0200
    Re: comparing dates? Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-02 09:58 -0400
    Re: comparing dates? Doug Miller <doug_at_milmacdotcom@example.com> - 2011-10-02 12:36 -0400
      Re: comparing dates? gordonb.wr5h4@burditt.org (Gordon Burditt) - 2011-10-02 23:54 -0500
        Re: comparing dates? Doug Miller <doug_at_milmacdotcom@example.com> - 2011-10-03 11:50 -0400
          Re: comparing dates? Jerry Stuckle <jstucklex@attglobal.net> - 2011-10-03 14:59 -0400
    Re: comparing dates? bobm3@worthless.info - 2011-10-03 20:31 -0400
    Re: comparing dates? Michael Joel <no@please.com> - 2011-10-04 09:27 -0400
      Re: comparing dates? The Natural Philosopher <tnp@invalid.invalid> - 2011-10-04 15:43 +0100
    Re: comparing dates? "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-10-06 11:00 +0200

#3258 — comparing dates?

FromMichael Joel <no@please.com>
Date2011-10-02 09:00 -0400
Subjectcomparing dates?
Message-ID<o2og875gpttj3c9h6ilv93mosd88dbujqt@4ax.com>
Is there a better way to compare dates than breaking a date into day,
month, year and comparing each part?
I need to know if a date (say 10/03/2011) is before another date in
the same format. Direct comparison doesn't work.

Thanks
Mike

[toc] | [next] | [standalone]


#3260

Fromhoughi <houghi@houghi.org.invalid>
Date2011-10-02 15:43 +0200
Message-ID<slrnj8gqk1.45r.houghi@penne.houghi>
In reply to#3258
Michael Joel wrote:
> Is there a better way to compare dates than breaking a date into day,
> month, year and comparing each part?
> I need to know if a date (say 10/03/2011) is before another date in
> the same format. Direct comparison doesn't work.

strtotime, but mind the layout of your date. For me the above would be
tenth of march, for others it may be october 3rd.

<?php
echo strtotime("10/03/2011"), "\n";
?>

That should be easy enough to make comparisons
http://php.net/manual/en/function.strtotime.php

houghi
-- 
How do you ask a man to be the last man to die in Iraq?
How do you ask a man to be the last man to die for a mistake?

[toc] | [prev] | [next] | [standalone]


#3261

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-10-02 09:58 -0400
Message-ID<j69qll$37b$2@dont-email.me>
In reply to#3258
On 10/2/2011 9:00 AM, Michael Joel wrote:
> Is there a better way to compare dates than breaking a date into day,
> month, year and comparing each part?
> I need to know if a date (say 10/03/2011) is before another date in
> the same format. Direct comparison doesn't work.
>
> Thanks
> Mike

If the dates are since 1/1/1970 you can use strtotime() to convert to a 
unix date/time and compare directly.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#3266

FromDoug Miller <doug_at_milmacdotcom@example.com>
Date2011-10-02 12:36 -0400
Message-ID<j6a3ut$rga$1@dont-email.me>
In reply to#3258
On 10/2/2011 9:00 AM, Michael Joel wrote:
> Is there a better way to compare dates than breaking a date into day,
> month, year and comparing each part?
> I need to know if a date (say 10/03/2011) is before another date in
> the same format. Direct comparison doesn't work.

Direct comparison certainly does work, as long as you have it formatted 
as year-month-day.

[toc] | [prev] | [next] | [standalone]


#3275

Fromgordonb.wr5h4@burditt.org (Gordon Burditt)
Date2011-10-02 23:54 -0500
Message-ID<zIqdnS_7npOS3RTTnZ2dnUVZ_oudnZ2d@posted.internetamerica>
In reply to#3266
>> I need to know if a date (say 10/03/2011) is before another date in
>> the same format. Direct comparison doesn't work.
> 
> Direct comparison certainly does work, as long as you have it formatted 
> as year-month-day.

Careful.

"2011-10-03" compares less than "2011-3-7" if you use a straight
string comparison.

If you require a sufficient number of leading zeroes, and a consistent
separator, it will work OK.  But it's also likely to have a Y10K
problem.


[toc] | [prev] | [next] | [standalone]


#3293

FromDoug Miller <doug_at_milmacdotcom@example.com>
Date2011-10-03 11:50 -0400
Message-ID<j6cljm$r7s$1@dont-email.me>
In reply to#3275
On 10/3/2011 12:54 AM, Gordon Burditt wrote:
>>> I need to know if a date (say 10/03/2011) is before another date in
>>> the same format. Direct comparison doesn't work.
>>
>> Direct comparison certainly does work, as long as you have it formatted
>> as year-month-day.
>
> Careful.
>
> "2011-10-03" compares less than "2011-3-7" if you use a straight
> string comparison.

But those aren't the same format.
>
> If you require a sufficient number of leading zeroes, and a consistent
> separator, it will work OK.  But it's also likely to have a Y10K
> problem.

I think it's a pretty safe bet that software developed today will not be 
in use 7989 years later.

[toc] | [prev] | [next] | [standalone]


#3295

FromJerry Stuckle <jstucklex@attglobal.net>
Date2011-10-03 14:59 -0400
Message-ID<j6d0mj$csg$2@dont-email.me>
In reply to#3293
On 10/3/2011 11:50 AM, Doug Miller wrote:
> On 10/3/2011 12:54 AM, Gordon Burditt wrote:
>>>> I need to know if a date (say 10/03/2011) is before another date in
>>>> the same format. Direct comparison doesn't work.
>>>
>>> Direct comparison certainly does work, as long as you have it formatted
>>> as year-month-day.
>>
>> Careful.
>>
>> "2011-10-03" compares less than "2011-3-7" if you use a straight
>> string comparison.
>
> But those aren't the same format.
>>
>> If you require a sufficient number of leading zeroes, and a consistent
>> separator, it will work OK. But it's also likely to have a Y10K
>> problem.
>
> I think it's a pretty safe bet that software developed today will not be
> in use 7989 years later.
>

I don't know, Doug.  That's what they said in the 60's about Yr2K.

Code doesn't become obsolete - it just keeps getting modified :)

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#3298

Frombobm3@worthless.info
Date2011-10-03 20:31 -0400
Message-ID<qtkk87tt65qtg35f27ljrqj4o264gi0lm2@4ax.com>
In reply to#3258
On Sun, 02 Oct 2011 09:00:10 -0400, Michael Joel <no@please.com>
wrote:

>Is there a better way to compare dates than breaking a date into day,
>month, year and comparing each part?
>I need to know if a date (say 10/03/2011) is before another date in
>the same format. Direct comparison doesn't work.
>
>Thanks
>Mike

How about converting each date to julian format (ccyyddd).  I believe
the date function will do that and its a direct numeric comparison.

[toc] | [prev] | [next] | [standalone]


#3300

FromMichael Joel <no@please.com>
Date2011-10-04 09:27 -0400
Message-ID<ee2m8716c3jsarko5vmdn3rbtuitisui8f@4ax.com>
In reply to#3258
People weren't thinking code from 1960s would still be in use to 2000
so you had a "Y2K scare" - didn't really pan out to be a REAL problem.
Say that somehow humans do keep things going for another 7000 years -
I don't think comparing a 40 year span to a 7000 year span is
realistic. Did anyone actually see code from 1960 in 2000? Or is that
like th eY2K thing mostly stories heard from someone who heard from
someone?

Anyway :)
I ended up deciding to use the time stamp (mktime) to compare dates.

[toc] | [prev] | [next] | [standalone]


#3302

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2011-10-04 15:43 +0100
Message-ID<j6f634$ej$1@news.albasani.net>
In reply to#3300
Michael Joel wrote:
> People weren't thinking code from 1960s would still be in use to 2000
> so you had a "Y2K scare" - didn't really pan out to be a REAL problem.
> Say that somehow humans do keep things going for another 7000 years -
> I don't think comparing a 40 year span to a 7000 year span is
> realistic. Did anyone actually see code from 1960 in 2000? Or is that
> like th eY2K thing mostly stories heard from someone who heard from
> someone?
> 
> Anyway :)
> I ended up deciding to use the time stamp (mktime) to compare dates.
> 
I am sure at some stage there wont be any 32 bit timestamps left...so 
that takes us up to the year 584942419325 AD.


Comfortably longer then the lifetime of the earth.

[toc] | [prev] | [next] | [standalone]


#3317

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2011-10-06 11:00 +0200
Message-ID<j6jqmi$4d6$1@dont-email.me>
In reply to#3258
El 02/10/2011 15:00, Michael Joel escribió/wrote:
> Is there a better way to compare dates than breaking a date into day,
> month, year and comparing each part?
> I need to know if a date (say 10/03/2011) is before another date in
> the same format. Direct comparison doesn't work.

Doing maths with strings is normally complicated. PHP provides two 
native formats to handle dates: Unix timestamps and DateTime objects. 
Both allow direct comparison with the "<" operator.

var_dump(new DateTime('2011-03-10') < new DateTime('2011-03-31'));
var_dump(strtotime('2011-03-10') < strtotime('2011-03-31'));

bool(true)
bool(true)

Full reference can be found at 
http://es2.php.net/manual/en/book.datetime.php

-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web