FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » comparing dates?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
comparing dates? [message #175485] Sun, 02 October 2011 13:00 Go to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
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
Re: comparing dates? [message #175487 is a reply to message #175485] Sun, 02 October 2011 13:43 Go to previous messageGo to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
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?
Re: comparing dates? [message #175488 is a reply to message #175485] Sun, 02 October 2011 13:58 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: comparing dates? [message #175493 is a reply to message #175485] Sun, 02 October 2011 16:36 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
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.
Re: comparing dates? [message #175502 is a reply to message #175493] Mon, 03 October 2011 04:54 Go to previous messageGo to next message
gordonb.wr5h4 is currently offline  gordonb.wr5h4
Messages: 1
Registered: October 2011
Karma: 0
Junior Member
>> 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.
Re: comparing dates? [message #175520 is a reply to message #175502] Mon, 03 October 2011 15:50 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
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.
Re: comparing dates? [message #175522 is a reply to message #175520] Mon, 03 October 2011 18:59 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
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(at)attglobal(dot)net
==================
Re: comparing dates? [message #175525 is a reply to message #175485] Tue, 04 October 2011 00:31 Go to previous messageGo to next message
bobmct is currently offline  bobmct
Messages: 16
Registered: September 2010
Karma: 0
Junior Member
On Sun, 02 Oct 2011 09:00:10 -0400, Michael Joel <no(at)please(dot)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.
Re: comparing dates? [message #175527 is a reply to message #175485] Tue, 04 October 2011 13:27 Go to previous messageGo to next message
Michael Joel is currently offline  Michael Joel
Messages: 42
Registered: October 2011
Karma: 0
Member
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.
Re: comparing dates? [message #175529 is a reply to message #175527] Tue, 04 October 2011 14:43 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
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.
Re: comparing dates? [message #175544 is a reply to message #175485] Thu, 06 October 2011 09:00 Go to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
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
--
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: simple image in PHP
Next Topic: Question about new lines
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 02:32:46 GMT 2024

Total time taken to generate the page: 0.02509 seconds