Re: Date +30 comparison

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>
> I'm really struggling with dates in PHP. (Yes, I tried reading the
> manual)...
>
> Can someone provide code that does this:
>
> Takes current date, assigns it to a variable (let's say $today)
> Then adds 30 days to $today variable
> Takes a string ($nexteval) like '8/26/2009' and compare it to $today.
>
> The variable $nexteval must be greater than $today (which is today + 30
> days) or a message is echoed.
>
> I'm finding this difficult to do, but I'm coming from an ASP background.
>
> Any help appreciated.
>
David,

Look up date() and mktime() in the manual.

To get today's date is easy: date('m/d/Y');

But when wanting to add days/months/years to a date, you need to use
mktime()

mktime() breaks apart a date into hours/minutes/seconds/months/days/years.

Therefore I always break up my date into these types fo sections.

$month = date('m');
$day = date('d');
$year = date('Y');

Now that I have them seperated I create a variable $future_date and assign
it the value I want. In your case, 30 days in the future.

$future_date = mktime(0,0,0,date($month),date($day)+30,date($year));

Now I put it back into a date format.

$future_date = date('m/d/Y',$future_date);

echo $future_date;

Today is September 1st and the value of $future_date is October 1st because
there are 30 days in Spetember.

Now you can compare your dates.

if ($nexteval > $future_date) {
  echo "This date has already passed";
} else {
  *do some processing*
}

Hope that helps.

Dan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux