Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP YEAR CODE .. small question

Options
  • 10-03-2010 11:38am
    #1
    Registered Users Posts: 771 ✭✭✭


    Hi,
    i came across the following in some source code i have to edit.

    [PHP]$year = intval(substr($name, -4, 4));
    // put hard limits on the year - nothing in the future for us!
    $year = (($year <= date('Y')) && ($year > 2006) ) ? $year : date('Y') ;
    [/PHP]

    So as such the code eventually doesnt output anything for 2010..
    so im not a php expert by any means and have no idea what this code does.
    however i want to take out the hard limits of the year!

    i tried removing the entire line, but no joy.

    Can anyone help?
    many thanks


Comments

  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    What exactly is it that you're trying to do?

    I find that the easiest way to implement a date system is to supply the user with a drop down list for year month and day rather than a text field (this enforces your integrity rules and prevents out of scope values).

    But if I was going to test that the date value supplied from a text box a regexp is probably the easiest way to go.

    If you give me more info on what you're trying to do I might be able to help.

    Regards,
    RD


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Well that piece of code seem to be doing a check of its own.

    It takes a variable, $name, and gets the last 4 characters from that variable. Presumably a date is expected, like "01-01-2010"

    Then it checks that value. If that value is between 2006 and this year (inclusive), then it's kept. Otherwise the value is reset to this year.

    I would suggest that if nothing is coming out for 2010, then the problem is probably related to the code which causes output as opposed to this.
    I find that the easiest way to implement a date system is to supply the user with a drop down list for year month and day rather than a text field (this enforces your integrity rules and prevents out of scope values).
    Well it doesn't prevent out-of-scope values, it just makes it harder for a pleb to make a mistake. A malicious user can still provide out-of-scope values.

    More importantly what such an approach does it remove the guesswork out of date processing. If you supply a free-form text box and a value that comes in is "10/02/2009", is that the 10th of February or the 2nd of October?

    But if you supply drop-down boxes clearly labelled, then you don't have to try split, validate and recombine a free-form string which may or may not even be a valid date.
    Even if a malicious user sends through a date which looks like ";/DELETE ME/<binary string>", simple regexp and validation can reject it.


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Thanks Seamus for agreeing in the end with what I'm saying, though taking the opportunity to thrash my suggestion while doing it :)

    Again to the OP, some clarification on what you need to do so we can explain the easiest way to do it.

    -RD


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    I found this link handy for dates in PHP: http://www.kirupa.com/web/php_unix_timestamp.htm


Advertisement