Solved

Convert DD-MM-YYYY from PHP to YYYY-MM-DD in MySQL

Posted on 2010-11-20
9
608 Views
Last Modified: 2012-05-10
I have an update HTML form using PHP to submit birthdates to MySQL to a field birth_date as a DATE type.

The form can only send as YYYY-MM-DD to the database. I need to send as DD-MM-YYYY where the user will enter 1/1/1990 or 01/01/1990 but converts to YYYY-MM-DD to the database.

How do I do this? Code snippers are provided below. Your help with a solution using the code snippets will be appreciated. Thanks.
html

<form action="process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr>
<td>Birth Date</td>
<?php
if(trim($form->value("birth_date")) == ""){
?>
<td><input type="text" name="birth_date" maxlength="50" value="<?php echo $session->userinfo['birth_date'];
 ?>"></td>
<?php
}else{
?>
<td><input type="text" name="birth_date" maxlength="50" value="<?php  echo $form->value("birth_date"); ?>"></td>
<?php
}
?>
<td><? echo $form->error("birth_date"); ?></td>
</tr>
<input type="hidden" name="subedit" value="1">
<input type="submit" value="Edit Account"></td></tr>
</table>
</form>


php

class Process
{
function Process(){
      global $session;
if(isset($_POST['sublogin'])){
         $this->procLogin();
 else if(isset($_POST['subedit'])){
         $this->procEditAccount();
      }
  }
}

function procEditAccount(){
      global $session, $form;
      /* Account edit attempt */
      $retval = $session->editAccount(,,,$_POST['birth_date'],,,,);
   }
};

Open in new window

0
Comment
Question by:bianchef9
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
ID: 34182016

<?

$ymd=date("Y-m-d", strtotime($dmy));

?>

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 34182219
Hi bianchef9. I'm not sure on how your app works: you're using an oop design pattern and this makes difficult use correctly your code to answer your question. I post a little snippet to show how you can reformat a given string date.

<?php
$date = "01/01/1990";
$newdate = date("Y/m/d", strtotime($date));
echo "$date vs $newdate";
?>

So you should grab $_POST['birth_date'] and convert this way bewfore to insert or update your database

<?php
$birth_date = date("Y/m/d", strtotime($_POST['birth_date']));
//insert or update using $birth_date
?>

Cheers
0
 
LVL 1

Expert Comment

by:warhero
ID: 34182841
I suppose you already have the solution above, so i am just going tell you that if you still have questions, then go to this site and it will give you more insight on how php's date function work based on format

http://php.net/manual/en/function.date.php - Date function (here you can explore all the possibilites of formatting, may be for future referencing)
http://www.php.net/manual/en/function.strtotime.php - String to date function

anyway hope it helps.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:bianchef9
ID: 34183550
marqusG

I tested your code snippet and it did display

01/01/1990 vs 1990/01/01

but I need the
user to enter 1/1/1990 and
update database as 1990-01-01 and then
display back to the browser as 1/1/1990

marqusG and sudaraka

where do I place your code suggestions?

for example, it does not convert when I just insert code in user.php
which uses session.php as "class Session"
and uses process.php for "class Process"

See code excerpts from user.php, session.php and process.php below.
Thanks.
user.php

<tr>
<td>Birth Date</td>
<?php
if(trim($form->value("birth_date")) == ""){
$birth_date = date("Y/m/d", strtotime($_POST['birth_date'])); //* <-----CODE INSERT HERE?
?>
<td><input type="text" name="birth_date" maxlength="50" value="<?php echo $session->userinfo['birth_date'];
 ?>"></td>
<?php
}else{
?>
<td><input type="text" name="birth_date" maxlength="50" value="<?php  echo $form->value("birth_date"); ?>"></td>
<?php
}
?>
<td><? echo $form->error("birth_date"); ?></td>
</tr>



session.php

 function editAccount(,,,,,$subbirth_date,,,,,){
      global $database, $form;

......

 $field = "birth_date";
	  if($subbirth_date){
		  $database->updateUserField($this->username,"birth_date",$subbirth_date);
      } 


process.php

class Process
{
function Process(){
      global $session;
if(isset($_POST['sublogin'])){
         $this->procLogin();
 else if(isset($_POST['subedit'])){
         $this->procEditAccount();
      }
  }
}

......

function procEditAccount(){
      global $session, $form;
      /* Account edit attempt */
      $retval = $session->editAccount(,,,$_POST['birth_date'],,,,);
   }
};

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 34183694
Looking at your code it seems that birth_date is hold by $session->userinfo('birth_date'). So you can use this to convert date format as you wish. Simply place at the top of your files

if (isset($session->userinfo('birth_date'))){
  $session->userinfo('birth_date') = date("Y-m-d", $session->userinfo('birth_date'));
}

in the page that insert data in database and

if (isset($session->userinfo('birth_date'))){
  $session->userinfo('birth_date') = date("d/m/Y", $session->userinfo('birth_date'));
}

in the page where form is displayed

Hope this helps

Cheers
0
 
LVL 18

Accepted Solution

by:
Sudaraka Wijesinghe earned 250 total points
ID: 34183817
I assume the birth day value you insert to DB is in $subbirth_date?
See if this works.
user.php

<tr>
<td>Birth Date</td>
<?php
if(trim($form->value("birth_date")) == ""){
	$birth_date = $session->userinfo['birth_date'];
} else {
	$birth_date = $form->value("birth_date");
}
$birth_date = date("m/d/Y", strtotime($birth_date)); // MM/DD/YYYY format for display
?>
<td><input type="text" name="birth_date" maxlength="50" value="<?php echo $birth_date; ?>"></td>
<td><? echo $form->error("birth_date"); ?></td>
</tr>



session.php

 function editAccount(,,,,,$subbirth_date,,,,,){
      global $database, $form;

......

 $field = "birth_date";
          if($subbirth_date){
		$subbirth_date = date("Y-m-d", strtotime($subbirth_date)); // YYYY-MM-DD format to store in DB
                  $database->updateUserField($this->username,"birth_date",$subbirth_date);
      }

Open in new window

0
 

Author Comment

by:bianchef9
ID: 34184387
marqusG:

I got an error:

Fatal error: Can't use method return value in write context in /home/me/public_html/mine/include/session.php on line 2

Fatal error: Can't use method return value in write context in /home/me/public_html/mine/include/user.php on line 2
0
 

Author Closing Comment

by:bianchef9
ID: 34184390
Your solution worked perfectly. Thanks.
0
 
LVL 18

Expert Comment

by:Sudaraka Wijesinghe
ID: 34184613
Glad to help. Thanks for the points.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MYSQL responding very slow 3 50
Pull Variable from URL  Use php template 1 35
Make login page safer 3 34
How do I fix this error with quotation marks? 4 26
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question