Solved

how display and edit date format using PHP and MySQL

Posted on 2010-11-19
6
334 Views
Last Modified: 2012-05-10
I need to display(list) and edit(update) the birth date on a web page using PHP connected to a MySQL database.

All is working ok, that is ,  I connect to the database from a web page but it displays the date as yyyy-mm-dd instead of dd-mm-yyyy (see userinfo.php in the code box).

Also, I can  update the date but only if I type it as yyyy-mm-dd into the web form (see useredit.php in the code box).

What do I need to do?
useredit.php for html to edit(update)

<tr>
<td>Birth Date</td>
<td><input type="text" name="birth_date" maxlength="50" value="
<?
if($form->value("birth_date") == ""){
   echo $session->userinfo['birth_date'];
}else{
   echo $form->value("birth_date");
}
?>">
</td>
<td><? echo $form->error("birth_date"); ?></td>
</tr>

userinfo.php for html to display(list)

*/birth_date */
echo "<b>Birth Date</b> ".$reg_user_info['birth_date']."<br>";

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
6 Comments
 
LVL 3

Expert Comment

by:PorterGraphics
ID: 34175302
You can use date_format() to format the date.

PHP - date_format()
0
 
LVL 8

Expert Comment

by:wolfgang_93
ID: 34175316
I presume that somewhere in your code you have a SQL command passed
to MySQL to fetch the values into the form, for example:

  select ...,birth_date,... from ....;

It would fetch any date field in the default MySQL date format of yyyy-mm-dd.

To change the default date display, you can apply the date-format SQL function
to the birth_date field. For example:

  select ...,date-format(xx,'%m-%d-%Y'),... from ...
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34175339
'yyyy-mm-dd' is the standard format for dates in part because it is a 'sortable' format.  This page http://us3.php.net/manual/en/function.date.php gives a lot of info about the PHP 'date' function.  It can be used with the 'mktime' function http://us3.php.net/manual/en/function.mktime.php to re-arrange dates.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:bianchef9
ID: 34175434
wolfgang_93

I have SQL queries for several date fields as
SELECT ..., b_date, e_date, r_date, x_date, ... from

so do I need to use date-format for each date fields ?
is the syntax as date-format(b_date,'%m-$d-%Y') ?

also, how do use date-format for same several fields with INSERT and UPDATE queries?
0
 
LVL 8

Accepted Solution

by:
wolfgang_93 earned 250 total points
ID: 34175713
Yes, you would need to do the same thing for each date field that you are using
on the form.

To convert date format in an insert or update, you are generally converting
from string to a different date format, so you can use the MySQL str_to_date
function.

SQL example:
insert into kkk(xx) values(str_to_date('10-23-2010','%m-%d-%Y'));

PHP example is something like this I think:
$cmd="insert into ... values(... str_to_date('".$reg_user_info['birth_date']."','%m-%d-%Y')) ...";
0
 

Author Closing Comment

by:bianchef9
ID: 34180871
I used your suggestion for the following code which worked to display the human date from the server date.

/* Convert MySQL yyyy-mm-dd to PHP dd-mm-yyyy table contents */
$expiration_date=date("m/d/Y",strtotime($expiration_date));
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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.
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…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

724 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