From: Keary Suska <hierophant@pcisys.net>
To: Postgres-PHP <pgsql-php@postgresql.org>
Subject: Re: comparing dates
Date: Fri, 05 Jul 2002 09:01:18 -0600
Message-ID: <B94B10DE.103DA%hierophant@pcisys.net> (raw)
In-Reply-To: <1025901211.1846.73.camel@Askr>
on 7/5/02 2:33 PM, joseph@asti.dost.gov.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"
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-php@postgresql.org
Cc: hierophant@pcisys.net
Subject: Re: comparing dates
In-Reply-To: <B94B10DE.103DA%hierophant@pcisys.net>
References: <1025901211.1846.73.camel@Askr> <B94B10DE.103DA%hierophant@pcisys.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox