Solved

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

Posted on 2010-11-20
9
604 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

770 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