Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Date Format in PHP

Hi,

I am using a datepicker for a user to select a date, this date is displayed as dd/mm/yy in the form, when the data from the form is submitted it is not saved to the mysql database correctly. I have used the following code to format the date but this does not work
            $B_Date=date("Y-m-d",strtotime($B_Date));
            $S_Date=date("Y-m-d",strtotime($S_Date));

I'm I missing a line
<div id="content_wrapper">
<br/>
	<div class="onecolumn_wrapper">
	 <div class="onecolumn" style="margin-right:20%; margin-left:20%;">
	  <div class="content" >
      
    <?php	

		//code runs if the form has been submitted
		if (isset($_POST['submit'])) 

		{ 
		
		$p = $_POST['p'];
		$q = $_POST['q'];
		$btn= $_POST['btn'];
		
		//makes sure no blank fields 
		if (!$_POST['Name'] | !$_POST['Address'] ) 
		{
		die('You did not complete all of the required fields');
		}
		
		//insert into the database
		$_POST = array_map('mysql_real_escape_string',  $_POST);   
		
		$B_Date=date("Y-m-d",strtotime($B_Date));
		$S_Date=date("Y-m-d",strtotime($S_Date));

        $insert  =  "INSERT INTO teamdates
					(
					 Name,
					 Address,
					 City,
					 County,
					 Postcode,
					 Country,
					 B_Date,
					 S_Date,
					 H_Phone,
					 M_Phone,
					 Email,
					 Department,
					 EC1_Name,
					 EC1_Relationship,
					 EC1_Contact1,
					 EC1_Contact2,
					 EC2_Name,
					 EC2_Relationship,
					 EC2_Contact1,
					 EC2_Contact2
					 )
           VALUES  (
					'".$_POST['Name']."',
					'".$_POST['Address']."',
					'".$_POST['City']."',
					'".$_POST['County']."',
					'".$_POST['Postcode']."',
					'".$_POST['Country']."',
					'".$_POST['B_Date']."',
					'".$_POST['S_Date']."',
					'".$_POST['H_Phone']."',
					'".$_POST['M_Phone']."',
					'".$_POST['Email']."',
					'".$_POST['Department']."',
					'".$_POST['EC1_Name']."',
					'".$_POST['EC1_Relationship']."',
					'".$_POST['EC1_Contact1']."',
					'".$_POST['EC1_Contact2']."',
					'".$_POST['EC2_Name']."',
					'".$_POST['EC2_Relationship']."',
					'".$_POST['EC2_Contact1']."',
					'".$_POST['EC2_Contact2']."'										
					)";
		   	
		//print_r($_POST); 
        $add_member = mysql_query($insert);
       // echo $insert."<br>"; exit; 
        //$res = mysql_query($sql);

?>  


    <h1>Saved</h1>
    <p>Thank you, new details saved</a>.</p>

<?php 
		} 
		else 
		{ 
?>    
    <form action="/0176/web/main_page.php?p=5&q=4&btn=1" method="post">
	<input type="hidden" name="p" value="5" />
	<input type="hidden" name="q" value="4" />
	<input type="hidden" name="btn" value="1" />
    <h1> </h1>
    <table class="general">
        <tr>
          <td colspan="2"><div align="left">Name:</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="Name" id="Name" />
          </div></td>
          
          <td colspan="2"><div align="left">Address:</div></td>
          <th width="300" colspan="2"><div align="left">
            <input type="text" name="Address" id="Address" />
          </div></th>
        </tr> 
        <tr>
        
          <td colspan="2"><div align="left">City:</div></td>
          <td colspan="7"><div align="left">
            <input type="text" name="City" id="City" />
            </div>            <div align="left"></div></td>
          </tr>
        
        <tr>
          <td colspan="2"><div align="left">County:</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="County" id="County" />
          </div></td>
          
          <td colspan="2"><div align="left">Postcode:</div></td>
          <th colspan="2"><div align="left">
            <input type="text" name="Postcode" id="Postcode" />
          </div></th>
        </tr>
        <tr>
          <td colspan="2"><div align="left">Country:</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="Country" id="Country" />
          </div></td>
          <td colspan="2"><div align="left"></div></td>
          <th colspan="2"><div align="left">
          </div></th>
        </tr>
        <tr>
          <td colspan="2"><div align="left">Birthday:</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="B_Date" id="e_datepicker" />
          </div></td>
          <td colspan="2"><div align="left">Start Date:</div></td>
          <th colspan="2"><div align="left">
            <input type="text" name="S_Date" id="s_datepicker" />
          </div></th>
        </tr>
        <tr>
          <td colspan="2"><div align="left">Home Phone:</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="H_Phone" id="H_Phone" />
          </div></td>
          <td colspan="2"><div align="left">Mobile Phone:</div></td>
          <th colspan="2"><div align="left">
          <input type="text" name="M_Phone" id="M_Phone" />
          </div></th>
        </tr>
        <tr>
          <td colspan="2"><div align="left">Email:</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="Email" id="Email" />
          </div></td>
          <td colspan="2"><div align="left">Department</div></td>
          <th colspan="2"><div align="left">
            <input type="text" name="Department" id="Department" />
          </div></th>
        </tr>
        <tr>
          <td colspan="2"><div align="left">Emergency Contact 1</div></td>
          <td colspan="3"><div align="left"></div></td>
          <td colspan="2"><div align="left"></div></td>
          <th colspan="2"><div align="left"></div></th>
        </tr>
        <tr>
          <td colspan="2"><div align="left">Name</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="EC1_Name" id="EC1_Name" />
          </div></td>
          <td colspan="2"><div align="left">Relationship :</div></td>
          <th colspan="2"><div align="left">
            <input type="text" name="EC1_Relationship" id="EC1_Relationship" />
          </div></th> 					
	    </tr>
        <tr>
          <td colspan="2"><div align="left">Contact Tel 1 :</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="EC1_Contact1" id="EC1_Contact1" />
          </div></td>
          <td colspan="2"><div align="left">Contact Tel 2 :</div></td>
          <th colspan="2"><div align="left">
            <input type="text" name="EC1_Contact2" id="EC1_Contact2" />
          </div></th>
        </tr>
                <tr>
          <td colspan="2"><div align="left">Emergency Contact 2</div></td>
          <td colspan="3"><div align="left"></div></td>
          <td colspan="2"><div align="left"></div></td>
          <th colspan="2"><div align="left"></div></th>
        </tr>
        <tr>
          <td colspan="2"><div align="left">Name</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="EC2_Name" id="EC2_Name" />
          </div></td>
          <td colspan="2"><div align="left">Relationship :</div></td>
          <th colspan="2"><div align="left">
            <input type="text" name="EC2_Relationship" id="EC2_Relationship" />
          </div></th> 					
	    </tr>
        <tr>
          <td colspan="2"><div align="left">Contact Tel 1 :</div></td>
          <td colspan="3"><div align="left">
            <input type="text" name="EC2_Contact1" id="EC2_Contact1" />
          </div></td>
          <td colspan="2"><div align="left">Contact Tel 2 :</div></td>
          <th colspan="2"><div align="left">
            <input type="text" name="EC2_Contact2" id="EC2_Contact2" />
          </div></th>
        </tr>
	</table>

	<input name="submit" type="submit" class="inputbox2_button" id="submit" value="Save Team Details" />
      <br />
      </ul>
     </form>
    <?php

}

?>

</div>                                    
            </p> 

			
		  </div>
		 </div>
		</div>
	</div>
	<br/>
</div>

<br class="clear"/><br/>

Open in new window

0
AlexAmbler
Asked:
AlexAmbler
  • 4
1 Solution
 
Ray PaseurCommented:
On line 27, it appears that $B_Date is undefined ?
0
 
Ray PaseurCommented:
Try something more like this...
Line 27-28


		$B_Date=date("Y-m-d",strtotime($_POST["B_Date"]));
		$S_Date=date("Y-m-d",strtotime($_POST["S_Date"]));

Open in new window

0
 
Ray PaseurCommented:
I think the date picker format will work with strtotime, according to my test.
http://www.laprbass.com/RAY_strtotime.php?s=03%2F05%2F11

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Please post back if you have any questions about this.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
haloexpertsexchangeCommented:
try this  
 $B_Date=date("Y-m-d",strtotime($_POST["B_Date"]));
     $S_Date=date("Y-m-d",strtotime($_POST["S_Date"]));

Open in new window

since as far as I can tell the B_Date and the S_Date come from the form and there for come in as post values.
0
 
AlexAmblerAuthor Commented:
I have just modified the code and entered the date 27/05/11 in the form, this has saved as 2027-05-11
0
 
Ray PaseurCommented:
Interesting.  Your date-picker must think you are using dates like the ISO standard YYYY-mm-dd.  
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now