Solved

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

Posted on 2010-11-20
9
601 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
 

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now