Improve company productivity with a Business Account.Sign Up

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

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

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
bianchef9
Asked:
bianchef9
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Sudaraka WijesingheWeb Application ProgrammerCommented:

<?

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

?>

Open in new window

0
 
Marco GasiFreelancerCommented:
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
 
warheroCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
bianchef9Author Commented:
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
 
Marco GasiFreelancerCommented:
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
 
Sudaraka WijesingheWeb Application ProgrammerCommented:
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
 
bianchef9Author Commented:
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
 
bianchef9Author Commented:
Your solution worked perfectly. Thanks.
0
 
Sudaraka WijesingheWeb Application ProgrammerCommented:
Glad to help. Thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now