Forum Moderators: coopster

Message Too Old, No Replies

compare string in php

         

yllai

9:03 am on Jul 14, 2004 (gmt 0)

10+ Year Member



user will enter month ($month), year ($year), retention period ($retention).

in my system, the disposal date ($disposal)will caunt as below:-
$disposal_year=$year+$retention;
$disposal="$month, $disposal_year"

for example: user enter value $month=October, $year=2001, $retention=1. so $disposal_year will be 2002 and $disposal will be "October, 2002". IF today is "December, 2002"(make it as $today), so i want to campare $disposal with $today...how can i do it? is it can be done with php? any example?

ergophobe

4:25 pm on Jul 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Three ways:

1. store $disposal as a timestamp and compare with current time
2. convert disposal to timestamp and compare with current time
3. convert current time to string and compare with $disposal

That is in order from my preferred to least preferred method, but others may differ.

#3 is what you asked for. You could use current time to generate a string using the date() [php.net] function like so

$today = date("F, Y");

if ($disposal == $today) {get rid of it}

With that method, though, you must use "equals" not "Less than or equals".

#2. Since I want to be sure that I catch it no matter what, I would probably use something more like this:

$disposal_timestamp = strtotime [php.net]("1 " . $disposal);

if ($disposal_timestamp <= time()) {}

#1. All things considered, though, I would probably validate the date as carefully as possible the first time around, and then store it as a timestamp and then be able to quickly and easily compare with the current timestamp with no conversion. This could be a unix or a mysql timestamp depending on how things are set up.

Tom