Hi,

As part of my site I have a small jobs database. It's pretty simple with only a fwe fields:
id, company, jobtitle, jobdesc, contact, closingdate, dateadded.

At the moment it all works fine, with 'dateadded' being populated by the inimitable now() function🙂

What I need to do though is find some way of converting the closingdate to the same kind of format. This is entered by the user though so I need to be able to tell them to submit in a specific format (eg. 12/05/2006) and then let the php convert it to 0000-00-00 format. I've been all through the forums and while i've seen hints at what I need, I just can't quite piece it together and see how to do it.

I'd guess using the explode() function might be of use, but then I'm not sure how to then switch those array sections around before imploding them into a 2006-12-05 format.

Can someone please help, or point me in the direction of a decent tutorial? I've found masses on how to enter today's date, but not on selecting a date to be entered.

Cheers chaps!

    $date_entered = '12/05/2006';
    $closingdate = date('Y-m-d', strtotime($date_entered));
    echo $closingdate;
    

    this should work with just about any date format entered.

      Beautiful thanks devinemke - I looked at strtotime, but wasn't sure what the formatting restrictions were. I'll try that later!

      What would be the best way to validate that entry - would it be an ereg()?

        hiya,

        I've tried putting that in, but it's not quite working - i'm just getting 0000-00-00 returned as a value, it doesn't seem to be inserting properly, but I'm not sure why.

        Here's the code:

        if (trim($_POST['closingdate']) == '') {$errorlist[] = 'Invalid entry: closingdate';}
        
        //check for errors, if none found:
        
        if (sizeof($errorlist) == 0)
        {
        $connector = new DbConnector();
        
        $date_entered = '$closingdate'; 
        $closingdatefix = date('Y-m-d', strtotime($date_entered));
        $closepost = $_POST['closingdatefix'];
        
        $insertQuery="INSERT INTO jobs (company, jobtitle, jobdesc, contact, dateadded, closingdate) VALUES ('$company','$jobtitle', '$jobdesc', '$contact', NOW(), '$closepost')";
        $result = $connector->query($insertQuery) or die ("Error in query: $insertQuery.".mysql_error());
         

          i don't know the rest of your form/error checking routine but here is the basic idea. note the use of [man]checkdate[/man], so someone can't enter a date like 2/31/2005.

          $errorlist = array();
          $closingdate = trim($_POST['closingdate']);
          $timestamp = strtotime($closingdate);
          if (!$closingdate || !checkdate(date('m', $timestamp), date('d', $timestamp), date('Y', $timestamp))) {$errorlist[] = 'Invalid entry: closingdate';}
          
          if (!$errorlist)
          {
          	$connector = new DbConnector();
          	$closingdate = date('Y-m-d', $timestamp); 
          	$insertQuery = "INSERT INTO jobs (company, jobtitle, jobdesc, contact, dateadded, closingdate) VALUES ('$company','$jobtitle', '$jobdesc', '$contact', NOW(), '$closingdate')";
          	$result = $connector->query($insertQuery) or die ("Error in query: $insertQuery.".mysql_error());
          }
          

            I'm probably being really dumb here, but in my SQL query you've changed $closepost to $closingdate, but isn't $closingdate what I originally had before it was passed for correction by $timestamp?

            Would I not want to be inserting $timestamp?

              TobyRT wrote:

              I'm probably being really dumb here, but in my SQL query you've changed $closepost to $closingdate, but isn't $closingdate what I originally had before it was passed for correction by $timestamp?

              no. not after this line:

              $closingdate = date('Y-m-d', $timestamp);  

                Cheers for that devinemke - it adds a treat.

                The problem now though is that the validation doesn't seem to be working - if I enter anything at all in that 'closingdate' fieldit says "update successful", but shows 0000-00-00

                Here's the full code for the page:

                <?php
                //form not yet submitted
                //display initial form
                
                if (!$_POST['submit'])
                {
                ?>
                
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <table width=100% align="center">
                <tr>
                	<td>Company</td>
                	<td><input type="text" name="company" size="60"></td>
                </tr>
                <tr>
                	<td>Job Title
                	<td><input type="text" name="jobtitle" size="60"></td>
                </tr>
                <tr>
                	<td>Job Description
                	<td><textarea name="jobdesc" cols="60" rows="20"></textarea></td>
                </tr>
                <tr>
                	<td>Contact/How to apply</td>
                	<td><textarea name="contact" cols="60" rows="20"></textarea></td>
                </tr>
                <tr>
                	<td>Closing Date: (Enter in format dd/mm/yyyy)</td>
                	<td><input type="text" name="closingdate" size="60"></td>
                </tr>
                
                </table>
                <div align="center"><input type="Submit" name="submit" value="Add"></div>
                </form>
                
                <?php
                }else{
                
                // Get the PHP file containing the DbConnector class
                require_once('../sypphp/sypcms/DbConnector.php');
                
                //set up error list array
                $errorlist = array();
                
                $company = $_POST['company'];
                $jobtitle = $_POST['jobtitle'];
                $jobdesc = $_POST['jobdesc'];
                $contact = $_POST['contact'];
                $closingdate = trim($_POST['closingdate']); 
                
                //validate the fields
                if (trim($_POST['company']) == '') {$errorlist[] = 'Invalid entry: company';}
                if (trim($_POST['jobtitle']) == '') {$errorlist[] = 'Invalid entry: jobtitle';}
                if (trim($_POST['jobdesc']) == '') {$errorlist[] = 'Invalid entry: jobdesc';}
                if (trim($_POST['contact']) == '') {$errorlist[] = 'Invalid entry: contact';}
                if (trim($_POST['closingdate']) == '') {$errorlist[] = 'Invalid entry: closingdate';}
                $timestamp = strtotime($closingdate); 
                if (!$closingdate || !checkdate(date('m', $timestamp), date('d', $timestamp), date('Y', $timestamp))) {$errorlist[] = 'Invalid entry: closingdate';} 
                //check for errors, if none found:
                //new query
                
                if (sizeof($errorlist) == 0)
                { 
                    $connector = new DbConnector(); 
                    $closingdate = date('Y-m-d', $timestamp);  
                
                $insertQuery = "INSERT INTO jobs (company, jobtitle, jobdesc, contact, dateadded, closingdate) VALUES ('$company','$jobtitle', '$jobdesc', '$contact', NOW(), '$closingdate')"; 
                $result = $connector->query($insertQuery) or die ("Error in query: $insertQuery.".mysql_error()); 
                
                //print result
                echo 'Update Successful<br><a href=jobsadmin.php>Click here to return to the jobs admin menu</a>';
                
                }else{
                
                //errors found: show them in a list
                echo 'The following errors have been encountered:<br>';
                for ($x=0; $x<sizeof($errorlist); $x++)
                {
                echo $errorlist [$x];
                }
                }
                }
                ?>
                  <?php
                  if (!isset($_POST['submit']))
                  {
                  	echo '
                  	<form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
                  	<table width="100%" align="center">
                  	<tr> 
                  	    <td>Company</td> 
                  	    <td><input type="text" name="company" size="60"></td> 
                  	</tr> 
                  	<tr> 
                  	    <td>Job Title 
                  	    <td><input type="text" name="jobtitle" size="60"></td> 
                  	</tr> 
                  	<tr> 
                  	    <td>Job Description 
                  	    <td><textarea name="jobdesc" cols="60" rows="20"></textarea></td> 
                  	</tr> 
                  	<tr> 
                  	    <td>Contact/How to apply</td> 
                  	    <td><textarea name="contact" cols="60" rows="20"></textarea></td> 
                  	</tr> 
                  	<tr> 
                  	    <td>Closing Date: (Enter in format dd/mm/yyyy)</td> 
                  	    <td><input type="text" name="closingdate" size="60"></td> 
                  	</tr> 
                  
                  </table> 
                  <div align="center"><input type="Submit" name="submit" value="Add"></div> 
                  </form>
                  ';
                  }
                  else
                  {
                  	require_once('../sypphp/sypcms/DbConnector.php');
                  	$errorlist = array();
                  
                  foreach ($_POST as $key => $value)
                  {
                  	$value = trim($value);
                  	if (!$value) {$errorlist[] = 'missing form field: ' . $key;}
                  	$$key = $value;
                  }
                  
                  $timestamp = strtotime($closingdate);
                  $getdate = getdate($timestamp);
                  if (!checkdate($getdate['mon'], $getdate['mday'], $getdate['year'])) {$errorlist[] = 'invalid closingdate';}
                  
                  if (!$errorlist)
                  {  
                  	$connector = new DbConnector();
                  	$closingdate = date('Y-m-d', $timestamp);
                  	$insertQuery = "INSERT INTO jobs (company, jobtitle, jobdesc, contact, dateadded, closingdate) VALUES ('$company', '$jobtitle', '$jobdesc', '$contact', NOW(), '$closingdate')";  
                  	$result = $connector->query($insertQuery) or exit('Error in query: ' . $insertQuery . ' ' . mysql_error());
                  	echo 'Update Successful<br><a href="jobsadmin.php">Click here to return to the jobs admin menu</a>';
                  }
                  else
                  {
                  	echo 'The following errors have been encountered:<br>'; 
                  	foreach ($errorlist as $value) {echo $value . '<br>';}
                  } 
                  }
                  ?>
                  

                    Thanks for that,

                    I'm just comparing your php with mine to try and work out exactly what it does, and I'm not quite sure.

                    Could you add some comments in there so I can learn from what you've given me pleeeeease?!

                      Ack!

                      I still can't get the consarned thing to work:

                      Here's the code:

                      // Get the PHP file containing the DbConnector class
                      require_once('../sypphp/sypcms/DbConnector.php');
                      
                      //set up error list array
                      $errorlist = array();
                      
                      $company = $_POST['company'];
                      $jobtitle = $_POST['jobtitle'];
                      $jobdesc = $_POST['jobdesc'];
                      $contact = $_POST['contact'];
                      $closingdate = $_POST['closingdate'];
                      
                      //validate the fields
                      if (trim($_POST['company']) == '') {$errorlist[] = 'You have not entered a company name';}
                      if (trim($_POST['jobtitle']) == '') {$errorlist[] = 'You have note entered a job title';}
                      if (trim($_POST['jobdesc']) == '') {$errorlist[] = 'You have note entered a job description';}
                      if (trim($_POST['contact']) == '') {$errorlist[] = 'You have note entered contact/application details';}
                      if (trim($_POST['closingdate']) == '') {$errorlist[] = 'Invalid entry: You have not entered a closingdate';}
                      
                      foreach ($_POST as $key => $value) 
                      { 
                      $value = trim($value); 
                      if (!$value) {$errorlist[] = 'missing form field: ' . $key;} 
                      $$key = $value; 
                      } 
                      
                      $timestamp = strtotime($closingdate); 
                      $getdate = getdate($timestamp); 
                      if (!checkdate($getdate['mon'], $getdate['mday'], $getdate['year'])) {$errorlist[] = 'invalid closingdate';} 
                      
                      if (!$errorlist) 
                      { 
                      $connector = new DbConnector(); 
                      $closingdate = date('Y-m-d', $timestamp); 
                      $insertQuery = "INSERT INTO jobs (company, jobtitle, jobdesc, contact, dateadded, closingdate) VALUES ('$company', '$jobtitle', '$jobdesc', '$contact', NOW(), '$closingdate')"; 
                      $result = $connector->query($insertQuery) or exit('Error in query: ' . $insertQuery . ' ' . mysql_error()); 
                      echo 'Update Successful<br><a href="jobsadmin.php">Click here to return to the jobs admin menu</a>'; 
                      } 
                      else 
                      { 
                      echo 'The following errors have been encountered:<br>'; 
                      foreach ($errorlist as $value) {echo $value . '<br>';} 
                      } 
                      } 
                      ?> 

                      The extra error validation now seems to be doing something weird to the date - i entered 29/04/2005 and it returned a date of 20th May 2004. It's getting confused somewhere and I just can't see where.

                      And it will still allow me to insert a random string of text instead of a valid date.

                      My head hurts.

                        Could anybody offer any more advise please?

                        I'm stumped!

                          Ah HA! I've fixed it.

                          I don't know why, but for some reason strtotime() didn't seem to work - jsut kept giving weird dates. I gave up on it in the end and used an implode/explode to get the date from 3 inputs rather than just one.

                          Here's the code for the implode and entry into the db:

                          $errorlist = array(); 
                          
                          $company = $_POST['company'];
                          $jobtitle = $_POST['jobtitle'];
                          $jobdesc = $_POST['jobdesc'];
                          $contact = $_POST['contact'];
                          $day = $_POST['day'];
                          $month = $_POST['month'];
                          $year = $_POST['year'];
                          
                          //validate the fields
                          if (trim($_POST['company']) == '') {$errorlist[] = 'You have not entered a Company';}
                          if (trim($_POST['jobtitle']) == '') {$errorlist[] = 'You have not entered a Job Title';}
                          if (trim($_POST['jobdesc']) == '') {$errorlist[] = 'You have not entered a Job Description';}
                          if (trim($_POST['contact']) == '') {$errorlist[] = 'You have not entered any Contact or Application details';}
                          if (trim($_POST['day']) == '') {$errorlist[] = 'You have not entered a day';}
                          if (trim($_POST['month']) == '') {$errorlist[] = 'You have not entered a month';}
                          if (trim($_POST['year']) == '') {$errorlist[] = 'You have not entered a year';}
                          if (!checkdate($_POST['month'], $_POST['day'], $_POST['year'])){$errorlist[] = 'Please enter a valid date';}
                          
                          
                          if (sizeof ($errorlist) == 0) 
                          { 
                          
                          $connector = new DbConnector(); 
                          $timestamp = array($year, $month, $day);
                          $closingdate = implode("-", $timestamp);
                          $insertQuery = "INSERT INTO jobs (company, jobtitle, jobdesc, contact, dateadded, closingdate) VALUES ('$company', '$jobtitle', '$jobdesc', '$contact', NOW(), '$closingdate')"; 
                          $result = $connector->query($insertQuery) or exit('Error in query: ' . $insertQuery . ' ' . mysql_error()); 
                          echo 'Update Successful<br><a href="jobsadmin.php">Click here to return to the jobs admin menu</a>'; 
                          

                          Of course, the next problem was how to get it out of the database and back INTO the form for the UPDATE page! For this I used an explode/list function:

                          //Get the date formatted & ready to insert
                          $closingdate = explode('-', $row['closingdate']);
                          list($year, $month, $day) = $closingdate;
                          

                          Now it's in the same format as the NEW page, and works with the same code. The only thing that changes is the SQL query.

                          I'm so glad I got around this - been a great learning experincee!

                            you could just completely eliminate the hassle and create 2 drop down boxes for month/day and then a 4 char text field.

                            Check the date to make sure feb and 31 aren't selected and you're good to go.

                              As far as I know the checkdate() function does that anyway.

                              Although it doesn't explain quite why it accepted 1234 as a valid year :queasy:

                              No matter, it's working for the time being, and by the time anyone in my society is good enough to hack my coding and crasht he site, I won't be a member anymore, so sod'em!!!

                                Write a Reply...