Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 33
  1. #16
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i somewhat understand what your saying, basically paymentDate or whatever the first variable is called, will grab my input using $_POST right ? , How would i put it in code so whatever i enter it checks if its between 2001 and 2004 ?

  2. #17
    New Coder
    Join Date
    Mar 2013
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post
    For displaying the message in php u should the echo or print statement . For eg:
    <?php echo " i am here"; ?>

  3. #18
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i know how to echo lol, i just can't put my logic into PHP code

  4. #19
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Anyone help a noob out

  5. #20
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What approach would you like to use?
    If you use a getdate, you can do so like this:
    PHP Code:
    $sDateGiven '31/12/2013';
    $aParts getdate(strtotime($sDateGiven));
    if (
    $aParts['year'] < $minYear || $aParts['year'] > $maxYear)
    {
        print 
    'That year is invalid.';

    DateTime will also work (baring any bugs and whatnots, which I know had some issues in older versions in regards to comparisons - just not offhand what they were). The only difference is you have to use midnight on it.

    Edit:
    Oops, my bad. The format of dd/M/y isn't for compounds of 31/12/2013 for example. Those are for 31/Dec/2013.
    So stick with either the datetime you have, or evaluate the string by pulling pieces off. Variables are simply a matter of setting them instead:
    PHP Code:

    $paymentDateString 
    '31/12/2010';
    $contractBeginString '01/01/2001';
    $contractEndString '01/01/2012';

    $paymentDate DateTime::createFromFormat('d/m/Y'$paymentDateString);
    $contractDateBegin DateTime::createFromFormat('d/m/Y'$contractBeginString);
    $contractDateEnd DateTime::createFromFormat('d/m/Y'$contractEndString);

    if (
    $paymentDate >= $contractDateBegin && $paymentDate <= $contractDateEnd)
    {
      echo 
    "is between\n";

    These can come from $_POST or $_GET or wherever as well. DateTime can also use the $dateTimeObject->format('Y') to get just the year and can be compared with simple checks: if ($dateTmeObject->format('Y') >= 2004) for example. The only negative side is that it is a string and not a number (but is a numeric string so evaluation shouldn't be an issue).

    Also, dateTime will throw an exception on failure. Therefore, for anything not using hardcoded data, you must use try/catch or it will throw an uncaught exception:
    PHP Code:
    $input '5th of smarch 2004';
    try
    {
        
    $mydt = new DateTime($input);
        
    // do more stuff here
    }
    catch (
    Exception $ex)
    {
        
    printf("There is an error in your date: %s"$ex->getMessage());

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  6. #21
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    would this bit of code be enough ??

    Code:
    $sDateGiven = '31/12/2013';
    $aParts = getdate(strtotime($sDateGiven));
    if ($aParts['year'] < $minYear || $aParts['year'] > $maxYear)
    {
        print 'That year is invalid.';
    }

  7. #22
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    No that won't work. The strtotime will fail on 31/12/2013 since there is no 31st month. I *thought* that was valid before, but upon inspecting it as d/M/Y, that would be 31/DEC/2013 that would be valid.

    To do this with 31/12/2013 requires either using the DateTime::createFromFormat method (5.3+), the use of strptime with a custom format (which you then calculate what you need for the year since it gives it as years since 1900), or explode to separate. The previous two can then be run against mktime functions to construct an integer timestamp which allows several ways to get to the actual year (although neither would be necessary since you need to calculate the year to build the timestamp).

    No doubt, PHP in my mind has the *worst* date handling ever. It is getting better mind you, ever since about 5.1 the entire datetime library is quite unstable which is great since pretty much every version we get something new in it. It's a shame I read the the compound representation wrong. It's amazing what a difference the 'm' and 'M' mean between the meanings.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  8. #23
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Not exactly sure what u mean fou-lou, Could you do it and explain every line so i get a better understanding please.. i've done the javascript for this part but the php im still getting my head around

  9. #24
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Anyone :P

  10. #25
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I'm not sure I understand your question.
    The problem is your date format for input isn't actually valid (despite that I originally thought it was) for use with the GNU formats. There are many many different ways to get to where you want though:
    PHP Code:
    // One way
    list($day$month$year) = explode('/'$sDate);

    // Another way
    $dt DateTime::createFromFormat('d/m/Y'$sDate);
    $year = (int)$dt->format('Y');

    // And another:
    $date strptime($sDate'%d/%m/%Y');
    $year 1900 $date['tm_year']; 
    And there are a few more ways than this as well. It depends on how 'pure' you want to keep the date versus whether or not just plucking the string for the year out is sufficient.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  11. #26
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Basically all i want to do, is when a user enters a date of birth the date must be between day/month/year so anywhere from 1/1/2000 - 2004

  12. #27
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yeah any of the above will do that for you. It doesn't validate that the year is any good or try different ways to assemble it, but any of the above will suffice for that. Just assign whatever input variable you are using to $sDate and you can get the $year from it. Then just use an if check for if ($year >= $minYear && $year <= $maxYear) and declare the minYear and maxYear to whatever values you want it to range between.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  13. #28
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Min'd showing how the code would look giving me an e.g with a HTML form so i can understand what your saying

  14. #29
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Anyone :P

  15. #30
    Regular Coder
    Join Date
    Apr 2013
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yeah any of the above will do that for you. It doesn't validate that the year is any good or try different ways to assemble it, but any of the above will suffice for that. Just assign whatever input variable you are using to $sDate and you can get the $year from it. Then just use an if check for if ($year >= $minYear && $year <= $maxYear) and declare the minYear and maxYear to whatever values you want it to range between.
    Hey dude mind showing me how to do it with a HTML form as it's alot clearer fo me to understand what your saying. Still trying to get my head around it


 
Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •