runnerjp Posted June 3, 2009 Share Posted June 3, 2009 ok so for my form im getting users to entre the date in the db via textbox... im using the date validation shown below <?php function goof_check($dat,$titl,$locn,$desc,$itsok) { GLOBAL $itsok; $goof_msg = ""; // clear error message string on entry // pseudo-check on date information $pieces = explode("-", $dat); $yy = strlen($pieces[0]); $mm = strlen($pieces[1]); $dd = strlen($pieces[2]); if (($dd<2)|| ($mm<2) || ($yy<4) ) { $goof_msg = "Date error: yyyy-mm-dd format is required boss<br />"; } // if (strlen($titl)<10) { $goof_msg.="Title is too short<br />"; } //if (strlen($locn)<10) { $goof_msg.="Location is too short<br />"; } if (strlen($desc)<10) { $goof_msg.="Event description is too short<br />"; } if ($goof_msg) { echo "<p><font color='#cc3333'><strong>Data Entry Error[s]</strong></font><br>". $goof_msg. "</font></p>"; $itsok = "0"; } return $itsok; } ?> but if i add the date as how i would like it dd-mm-yy i get the error... the only way its letitng me entre it is via yyy-mm-dd , my question is how cna i chnage this? Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/ Share on other sites More sharing options...
Ken2k7 Posted June 3, 2009 Share Posted June 3, 2009 Change $yy = strlen($pieces[0]); $mm = strlen($pieces[1]); $dd = strlen($pieces[2]); To $yy = strlen($pieces[2]); $mm = strlen($pieces[1]); $dd = strlen($pieces[0]); Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-848563 Share on other sites More sharing options...
laffin Posted June 3, 2009 Share Posted June 3, 2009 ya see why ppl like regex if(!preg_match('/^(\d{2,2})-(\d{2,2})-(\d{4,4})$/',$dat,$pieces) { $goof_msg = "Date error: yyyy-mm-dd format is required boss<br />"; } bout the only difference is the $pieces, return an array of: 0: (01-05-2009) 1: (01) 2: (05) 3: (2009) yep, 0 index should be the same as $dat Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-848574 Share on other sites More sharing options...
runnerjp Posted June 3, 2009 Author Share Posted June 3, 2009 humm ok i now have a problem with displayign the events under the calander <?php $ev_dat = array(); for ($i=0;$i<32;$i++) { $ev_dat[$i]=0; } $now = date("Y-m-d", time()); list($ty, $tm, $td) = explode('-',$now); // ty=thisyear, etc. used for highlighting 'today' include("cal_parms.php"); // assorted configuration variables include($dat_names); // retrieved from cal_parms.php as a 'language' file if (!isset($_GET['m'])) { $m = date("m",mktime()); } else { $m = $_GET['m']; } if (!isset($_GET['y'])) { $y = date("Y",mktime()); } else { $y = $_GET['y']; } /*== get what weekday the first is on ==*/ $tmpd = getdate(mktime(0,0,0,$m,1,$y)); $month = $tmpd["month"]; $firstwday= $tmpd["wday"]; if ($firstDayIsMonday == 1) { if ($firstwday == 0) { $firstwday = 6; } else { $firstwday--; } } $lastday = mk_getLastDayofMonth($m,$y); /*== get the last day of the month ==*/ function mk_getLastDayofMonth($mon,$year) { for ($tday=28; $tday <= 31; $tday++) { $tdate = getdate(mktime(0,0,0,$mon,$tday,$year)); if ($tdate["mon"] != $mon) { break; } } $tday--; return $tday; } // compute range of dates for this month to match dates in database in the format yyyy-mm-dd if (strlen($m)<2) { $q="0"; $q.=$m; } else { $q = $m; } $dats_beg = $y. "-". $q. "-01"; $dats_en = $lastday. "-". $q. "-".$y ; // open db conn and select all records where date is between $dats_beg and $dats_en include("cal_db_conn.php"); mysql_connect($db_host, $db_login, $db_pass) or die ("Can't connect!"); mysql_select_db($db_name) or die ("Can't open database!"); $query = "SELECT * FROM $db_table WHERE (ev_dat>='$dats_beg') AND (ev_dat<='$dats_en') "; $result = mysql_db_query($db_name, $query); ?> instead of gaining the months form the db as / compute range of dates for this month to match dates in database in the format yyyy-mm-dd they are now stored as dd-mm-yyyy so what would i need to chnage to find this? Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-848584 Share on other sites More sharing options...
runnerjp Posted June 3, 2009 Author Share Posted June 3, 2009 wait got it forgot to chnage $dats_beg = $y. "-". $q. "-01"; Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-848586 Share on other sites More sharing options...
runnerjp Posted June 3, 2009 Author Share Posted June 3, 2009 OK THAT DIDNT DO IT! so if i have chnaged the data to dd-mm-yyyy what would i need to chnage this too so it now works with dd-mm-yyy to find events held on that day?? <?php // compute range of dates for this month to match dates in database in the format yyyy-mm-dd if (strlen($m)<2) { $q="0"; $q.=$m; } else { $q = $m; } $dats_beg = $y. "-". $q. "-01"; $dats_en = $y. "-". $q. "-". $lastday; // open db conn and select all records where date is between $dats_beg and $dats_en include("cal_db_conn.php"); mysql_connect($db_host, $db_login, $db_pass) or die ("Can't connect!"); mysql_select_db($db_name) or die ("Can't open database!"); $query = "SELECT * FROM $db_table WHERE (ev_dat>='$dats_beg') AND (ev_dat<='$dats_en') "; $result = mysql_db_query($db_name, $query); // any matches? if ($result) { // handle the matches and pass relevant info to arrays while ($myrow = mysql_fetch_array($result)) { $found = $myrow['ev_dat']; $pieces = explode("-", $found); $dd = intval($pieces[2]); $ev_dat[$dd] = $myrow['id']; } } ?> <table cellpadding="1" cellspacing="1" border="0" bgcolor="#<? echo $bg_edge; ?>"> <tr><td colspan="7" bgcolor="#<? echo $bg_top; ?>"> <table cellpadding="1" cellspacing="1" border="0" width="100%"> <tr bgcolor="#<? echo $bg_top; ?>"><th width="20" style="<?php echo $hcell; ?>"><a href="<? echo $_SERVER['PHP_SELF']; ?>?m=<?=(($m-1)<1) ? 12 : $m-1 ?>&y=<?=(($m-1)<1) ? $y-1 : $y ?>"><img src='http://www.runningprofiles.com/calendar/images/prev.gif' height='18' width='18' alt='' border='0' /></a></th> <th style="<?php echo $hcell; ?>"> <?php echo "<a href='../members/diary/show-month.php?mon=". $m. "&yr=". $y. "'rel=\"facebox\">"; echo "<span style='text-decoration:none'>". $mo[intval($m)]. " ". $y. "</span></a>"; ?> </th> <th width="20" style="<? echo $hcell; ?>"><a href="<? echo $_SERVER['PHP_SELF']; ?>?m=<?=(($m+1)>12) ? 1 : $m+1 ?>&y=<?=(($m+1)>12) ? $y+1 : $y ?>"><img src='http://www.runningprofiles.com/calendar/images/next.gif' height='18' width='18' border='0' alt='' /></a></th> </tr> </table> </td></tr> <tr bgcolor="#<? echo $bg_top; ?>"> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[1]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[2]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[3]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[4]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[5]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[6]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[7]; ?></th> </tr> <? $d = 1; $wday = $firstwday; $firstweek = true; /*== loop through all the days of the month ==*/ while ( $d <= $lastday) { /*== set up blank days for first week ==*/ if ($firstweek) { if ($wday!=0) { echo "<tr bgcolor='#". $bg_tabl. "'>\n"; for ($i=1; $i<=$firstwday; $i++) { echo "<td style='". $tcell. "' bgcolor='#". $bg_fill. "'> </td>\n"; } } /*== Sunday start week with <tr> ==*/ else { echo "<tr bgcolor='#". $bg_tabl. "'>\n"; } $firstweek = false; } /*== check for event ==*/ echo "<td style='". $tcell. "' "; // is this day 'today' AND there's no event today if (($ty==$y) && ($tm==$m) && ($td == $d) && (!$ev_dat[$d])) { echo "bgcolor='#". $bg_now. "'>". $d; } elseif ($ev_dat[$d]) { // get what's happening that day and use as 'mouseOver' for the link $query = "SELECT * FROM $db_table WHERE id=$ev_dat[$d] "; $result = mysql_query($query); $ev = mysql_fetch_array($result); $titl = $ev['ev_title']; echo "bgcolor='#". $bg_act. "'>"; $url = "../members/diary/show.php?event=". $ev_dat[$d]. "&sho=". $win_sho; echo "<a href=' $url' rel=\"facebox\" title=\"". $titl. "\">". $d. "</a>"; } else { echo "bgcolor='#". $bg_days. "'>". $d; } echo "</td>\n"; /*== Saturday end week with </tr> ==*/ if ($wday==6) { echo "</tr>\n"; } $wday++; $wday = $wday % 7; if (($wday==0) AND ($d!=$lastday)){ echo "<tr bgcolor='#". $bg_tabl. "'>\n"; } $d++; } // and close off the table if (($wday!=7) AND ($wday!=0)) { for ($i=$wday; $i<=6; $i++) { echo "<td style='". $tcell. "' bgcolor='#". $bg_fill. "'> </td>\n"; } echo "</tr>"; } echo "\n</table>"; include("win_open.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-848709 Share on other sites More sharing options...
runnerjp Posted June 4, 2009 Author Share Posted June 4, 2009 bmp Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-849081 Share on other sites More sharing options...
otuatail Posted June 4, 2009 Share Posted June 4, 2009 Whould it help if you had 3 drop down boxes for day month and year. There are too many problems. Americans tend to use mm-d-yyyy and you are assumming the user will enter "-" and not "/". Dates are difficult. Desmond. Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-849088 Share on other sites More sharing options...
BioBob Posted June 4, 2009 Share Posted June 4, 2009 I suggest using checkdate() http://us2.php.net/checkdate bool checkdate ( int $month , int $day , int $year ) Returns TRUE if the date given is valid; otherwise returns FALSE. <?php var_dump(checkdate(12, 31, 2000)); var_dump(checkdate(2, 29, 2001)); ?> The above example will output: bool(true) bool(false) Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-849153 Share on other sites More sharing options...
runnerjp Posted June 4, 2009 Author Share Posted June 4, 2009 im more looking to chnage the code allready in place as the system works fine, but form feedback iv been asked to chnage it too dd-mm-yyyy as the main base is UK. Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-849231 Share on other sites More sharing options...
runnerjp Posted June 4, 2009 Author Share Posted June 4, 2009 ok i dont get it. in my db i have an event called test stored as 03-06-2009 but if you look at this link http://www.runningprofiles.com/members/diary/cal_show.php?m=6&y=2009 its displayed on every month on the 3rd date :S <?php $ev_dat = array(); for ($i=0;$i<32;$i++) { $ev_dat[$i]=0; } $now = date("d-m-Y", time()); list($td, $tm, $ty) = explode('-',$now); // ty=thisyear, etc. used for highlighting 'today' include("cal_parms.php"); // assorted configuration variables include($dat_names); // retrieved from cal_parms.php as a 'language' file if (!isset($_GET['m'])) { $m = date("m",mktime()); } else { $m = $_GET['m']; } if (!isset($_GET['y'])) { $y = date("Y",mktime()); } else { $y = $_GET['y']; } /*== get what weekday the first is on ==*/ $tmpd = getdate(mktime(0,0,0,$m,1,$y)); $month = $tmpd["month"]; $firstwday= $tmpd["wday"]; if ($firstDayIsMonday == 1) { if ($firstwday == 0) { $firstwday = 6; } else { $firstwday--; } } $lastday = mk_getLastDayofMonth($m,$y); /*== get the last day of the month ==*/ function mk_getLastDayofMonth($mon,$year) { for ($tday=28; $tday <= 31; $tday++) { $tdate = getdate(mktime(0,0,0,$mon,$tday,$year)); if ($tdate["mon"] != $mon) { break; } } $tday--; return $tday; } // compute range of dates for this month to match dates in database in the format yyyy-mm-dd if (strlen($m)<2) { $q="0"; $q.=$m; } else { $q = $m; } $dats_beg = "01-". $q."-". $y ; $dats_en = $lastday . "-". $q. "-".$y ; // open db conn and select all records where date is between $dats_beg and $dats_en include("cal_db_conn.php"); mysql_connect($db_host, $db_login, $db_pass) or die ("Can't connect!"); mysql_select_db($db_name) or die ("Can't open database!"); $query = "SELECT * FROM $db_table WHERE (ev_dat>='$dats_beg') AND (ev_dat<='$dats_en') "; $result = mysql_db_query($db_name, $query); // any matches? if ($result) { // handle the matches and pass relevant info to arrays while ($myrow = mysql_fetch_array($result)) { $found = $myrow['ev_dat']; $pieces = explode("-", $found); $dd = intval($pieces[0]); $ev_dat[$dd] = $myrow['id']; } } ?> <table cellpadding="1" cellspacing="1" border="0" bgcolor="#<? echo $bg_edge; ?>"> <tr><td colspan="7" bgcolor="#<? echo $bg_top; ?>"> <table cellpadding="1" cellspacing="1" border="0" width="100%"> <tr bgcolor="#<? echo $bg_top; ?>"><th width="20" style="<?php echo $hcell; ?>"><a href="<? echo $_SERVER['PHP_SELF']; ?>?m=<?=(($m-1)<1) ? 12 : $m-1 ?>&y=<?=(($m-1)<1) ? $y-1 : $y ?>"><img src='http://www.runningprofiles.com/calendar/images/prev.gif' height='18' width='18' alt='' border='0' /></a></th> <th style="<?php echo $hcell; ?>"> <?php echo "<a href='../members/diary/show-month.php?mon=". $m. "&yr=". $y. "'rel=\"facebox\">"; echo "<span style='text-decoration:none'>". $mo[intval($m)]. " ". $y. "</span></a>"; ?> </th> <th width="20" style="<? echo $hcell; ?>"><a href="<? echo $_SERVER['PHP_SELF']; ?>?m=<?=(($m+1)>12) ? 1 : $m+1 ?>&y=<?=(($m+1)>12) ? $y+1 : $y ?>"><img src='http://www.runningprofiles.com/calendar/images/next.gif' height='18' width='18' border='0' alt='' /></a></th> </tr> </table> </td></tr> <tr bgcolor="#<? echo $bg_top; ?>"> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[1]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[2]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[3]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[4]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[5]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[6]; ?></th> <th width="20" style="<?php echo $hcell; ?>"><? echo $da[7]; ?></th> </tr> <? $d = 1; $wday = $firstwday; $firstweek = true; /*== loop through all the days of the month ==*/ while ( $d <= $lastday) { /*== set up blank days for first week ==*/ if ($firstweek) { if ($wday!=0) { echo "<tr bgcolor='#". $bg_tabl. "'>\n"; for ($i=1; $i<=$firstwday; $i++) { echo "<td style='". $tcell. "' bgcolor='#". $bg_fill. "'> </td>\n"; } } /*== Sunday start week with <tr> ==*/ else { echo "<tr bgcolor='#". $bg_tabl. "'>\n"; } $firstweek = false; } /*== check for event ==*/ echo "<td style='". $tcell. "' "; // is this day 'today' AND there's no event today if (($ty==$y) && ($tm==$m) && ($td == $d) && (!$ev_dat[$d])) { echo "bgcolor='#". $bg_now. "'>". $d; } elseif ($ev_dat[$d]) { // get what's happening that day and use as 'mouseOver' for the link $query = "SELECT * FROM $db_table WHERE id=$ev_dat[$d] "; $result = mysql_query($query); $ev = mysql_fetch_array($result); $titl = $ev['ev_title']; echo "bgcolor='#". $bg_act. "'>"; $url = "../members/diary/show.php?event=". $ev_dat[$d]. "&sho=". $win_sho; echo "<a href=' $url' rel=\"facebox\" title=\"". $titl. "\">". $d. "</a>"; } else { echo "bgcolor='#". $bg_days. "'>". $d; } echo "</td>\n"; /*== Saturday end week with </tr> ==*/ if ($wday==6) { echo "</tr>\n"; } $wday++; $wday = $wday % 7; if (($wday==0) AND ($d!=$lastday)){ echo "<tr bgcolor='#". $bg_tabl. "'>\n"; } $d++; } // and close off the table if (($wday!=7) AND ($wday!=0)) { for ($i=$wday; $i<=6; $i++) { echo "<td style='". $tcell. "' bgcolor='#". $bg_fill. "'> </td>\n"; } echo "</tr>"; } echo "\n</table>"; include("win_open.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-849297 Share on other sites More sharing options...
laffin Posted June 4, 2009 Share Posted June 4, 2009 <?php function datecheck($dat) { if(preg_match('/^(\d{1,2})[-\/](\d{1,2})[-\/](\d{4,4})/',$dat,$pieces)) { $mm=$pieces[1]; $dd=$pieces[2]; $yy=$pieces[3]; } elseif(preg_match('/^(\d{4,4})[-\/](\d{1,2})[-\/](\d{1,2})/',$dat,$pieces)) { $mm=$pieces[2]; $dd=$pieces[3]; $yy=$pieces[1]; } if(isset($mm) && !checkdate($mm,$dd,$yy)) unset($mm); return isset($mm)?"{$mm}-{$dd}-{$yy}":FALSE; } var_dump(datecheck('12-31-20001')); var_dump(datecheck('02-29-20001')); var_dump(datecheck('20001-01-15')); var_dump(datecheck('07-04-20010')); var_dump(datecheck('07-32-20010')); ?> This should work, with both formats and return a valid date in the format yyyy-mm-dd now for sql ya want to read this: DATE_FORMAT() so in yer queries ya wud use DATE_FORMAT($dat,'%Y-%m-%d') instead of $dat or ya could use the routine above to convert from MySQL to yer date format Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-849346 Share on other sites More sharing options...
runnerjp Posted June 4, 2009 Author Share Posted June 4, 2009 if i add the code above to the current code and where do i add DATE_FORMAT($dat,'%Y-%m-%d') sorry?? would this then convert from my db 2 the prefered output and show the correct date ? Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-849354 Share on other sites More sharing options...
laffin Posted June 4, 2009 Share Posted June 4, 2009 DATE_FORMAT is used in yer queries to MySQL u can have MySQL return in your format if you use that function. Read the link provided, and play with it the routine I posted, just validates the date in one of two formats (this is for php side of things) mm-dd-yyyy or yyyy-mm-dd and returns FALSE if the date is invalid format or just invalid (as per suggestion by BioBob) Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-849359 Share on other sites More sharing options...
runnerjp Posted June 4, 2009 Author Share Posted June 4, 2009 ok im onto it...thanks Quote Link to comment https://forums.phpfreaks.com/topic/160783-solved-validating-the-date/#findComment-849361 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.