View Full Version : if condition in php for a date in form input?

05-29-2011, 05:42 PM
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.

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
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.

05-29-2011, 06:48 PM
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).

$var = 'Sept 14, 1996';
$sFormat = 'Y-m-d';

$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;
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).

05-29-2011, 09:56 PM
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).