Re: comparing dates

From: Keary Suska <hierophant(at)pcisys(dot)net>
To: Postgres-PHP <pgsql-php(at)postgresql(dot)org>
Subject: Re: comparing dates
Date: 2002-07-05 15:01:18
Message-ID: B94B10DE.103DA%hierophant@pcisys.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php

on 7/5/02 2:33 PM, joseph(at)asti(dot)dost(dot)gov(dot)ph purportedly said:

> how do i compare the current date to a date field in my database
> e.g.
> $current_date = date('Y=m-d');
> $dbdate = pg_result($rsdate,0,"exam_date");
> if ($current_date>$dbdate) --THIS PART DOESNT WORK
> echo "Today is not your exam date";
> TIA

You have encountered what I consider one of the most annoying "features" if
PHP. PHP is treating both values as strings, but when you do a comparison,
PHP looks to see if the value looks like a number. If it does, it treats
both as numbers, and discards any non-number portion. Because of this,
chances are, you end up only comparing the years (everything after the first
'-' is dropped). You have many options. Among them:

1) use stringcmp()
2) Make both into a number-looking string: date('Ymd') and str_replace( '-',
'', $dbdate )
3) convert to epochal time (seconds since epoch) using time() for current
time, and strtotime to convert the db date. Note, however, that strtotime()
cannot parse SQL dates because of the dashes. To get around this, you can:
strtotime( str_replace( '-', '/', $dbdate ) )

Keary Suska
Esoteritech, Inc.
"Leveraging Open Source for a better Internet"

In response to

Browse pgsql-php by date

  From Date Subject
Next Message Joseph Syjuco 2002-07-05 20:33:29 comparing dates
Previous Message Christopher Kings-Lynne 2002-07-05 08:33:58 Re: comparing dates