?
Solved

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

Posted on 2010-11-20
9
Medium Priority
?
613 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 1000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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