Question:

PHP code to check year?

by Guest65077  |  earlier

0 LIKES UnLike

Hi,

Can anyone tell me the php code to check that a variable submitted via a html form is a year between 1900 and the current year (taken from a time variable if poss).

To give you an idea of what I mean, here is the equivalent code in Ruby:

current = Time.new

if year < 1900 or jahr > current.year

$error += "You didn't enter a sensible year"

end

Thanx in advance

 Tags:

   Report

2 ANSWERS


  1. To get the current year, use PHP&#039;s date function:

    http://php.net/date

    $current_year = date(&#039;Y&#039;);

    The code would be similar to Ruby.

    if($year &lt; 1900 OR $year &gt; $current_year)

    {

    //Note the period before the equal sign. That&#039;s the

    //concatenation operator in PHP.

    $error .= &quot;You didn&#039;t enter a sensible year.&quot;

    }


  2. To get the current year you use the date(&quot;Y&quot;) command.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.