Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP YEAR CODE .. small question

  • 10-03-2010 11:38AM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 68,173 ✭✭✭✭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