Обсуждение: comparing dates

Поиск
Список
Период
Сортировка

comparing dates

От
Joseph Syjuco
Дата:
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





Re: comparing dates

От
"Christopher Kings-Lynne"
Дата:
Use mktime to convert them both to unix timestamps and then compare them as
integers, or compare them in the database directly.

Chris


> -----Original Message-----
> From: pgsql-php-owner@postgresql.org
> [mailto:pgsql-php-owner@postgresql.org]On Behalf Of Joseph Syjuco
> Sent: Saturday, 6 July 2002 4:33 AM
> To: PHP-PGSQL
> Subject: [PHP] comparing dates
>
>
> 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
>
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>
>




Re: comparing dates

От
Keary Suska
Дата:
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"