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.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    if condition in php for a date in form input?

    Hi,,
    Wondering what to use in the if condition for a date field in php when there is a form input involve?

    Some background information:
    1) Hacking a plugin to have a field of post expire. Myphpadmin shows field created with default assignment. Looks good.
    Code:
    if (!$exists) {
                $h->db->query("ALTER TABLE " . TABLE_POSTS . " ADD post_expire DATE NOT NULL DEFAULT '2003-01-01' AFTER post_comments");
     }
    2) The program uses an input form and it allows the user to edit the input fields.

    3) Var_dump (post_expire) of the date field is string(10) "0000-00-00"

    4) The plugin was initially set to have a price field and I am trying to modify to have a date field. Modify the price field from float to int works but doing for date is the problem for me. The plugin uses
    Code:
    if (!isset($price))
    to assign a default price value.

    Thus, my question is what to put in the if statement to test if date has a value cleanly (not to have the step 3 var dump value of 0000-00-00) and assign a DEFAULT '2003-01-01' if not define. Concern of null, 0 zeroes, spaces, and above var dump value.

    Please note I try reading isset, empty, and posted this question for my concerns as a newbie to have the proper if condition. I will do a TESTDATE prior to inserting into db. Appreciate your help and tx.
    Last edited by scrtmtl; 05-29-2011 at 05:47 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    isset is always a requirement, you need to use it to determine if anything was actually entered and submitted.
    Test your provided date for valid using a combination of the date() and strtotime(), or the DateTime class (if you're on a new enough version). Best to stick with gnu formats to avoid confliction (see http://php.ca/manual/en/datetime.formats.php for string formats of dates that PHP will understand using DateTime or strtotime).
    PHP Code:

    $var 
    'Sept 14, 1996';
    $sFormat 'Y-m-d';
    date_default_timezone_set('UTC');

    try
    {
        
    $dt = new DateTime($var);
        print 
    'OO: ' $dt->format($sFormat) . PHP_EOL;
    }
    catch (
    Exception $ex)
    {
        print 
    'Invalid date entered' PHP_EOL;
    }

    // Or procedurally using strtotime instead of DateTime[_create]
    $control PHP_VERSION '5.1.0' false : -1;
    if (
    $control !== ($dt strtotime($var)))
    {
        print 
    'Procedural: ' date($sFormat$dt) . PHP_EOL;
    }
    else
    {
        print 
    'Invalid date entered' PHP_EOL;

    The results of the $dt->format or the date($sFormat, $dt) will be the string representing the entered date. Use the else of the procedure or the catch to set the value to null for the insertion so the default is used instead (or use it to conditionally build the insertion fields).

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fou-Lu, tx u for your reply and being in Montreal, I hope u are not affected with the flooding. Dor jer ( phonetic attempt to say tx or ser ser).

    With reference to item 4, was thinking along the line of something in the condition of the if statement
    if ((!isset($price) || ($price == "0000-00-00")) { $price = '2003-01-01'};
    to assign a default date.

    Two items for me to ponder is your reply and it will take me some time like a week to digest for I am newbie. And will ponder on this also: $var = (isset($var) && $var) ? $var : 'new value';

    U have the languages unless u want to work in a mainframe environment with COBOL. Surprise u are into Fortran (watfiv).


  •  

    Posting Permissions

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