Solved

Displaying fomatted mysql date field

Posted on 2010-09-08
7
192 Views
Last Modified: 2012-06-22
in the code window is my code to sdisplay a form that allows a user to edit information, the last diaplayed form element is the Expiry date field, I have tried all sorts of ways to format it so that it displays as DD-MM-YYYY , but no luck, I am probably missing something simple.

Where does the date_format function come into play , before the form code or in the form element below?

<p>
            <label for="Expiry">Expiry <em>*</em></label>
            <input name="Expiry" id="Expiry" size="25" maxlength="100" class="required" value="' . $row_user_edit['Expiry'] . '"/>
            </p>

thanks in advance.
$user_edit = '<div><form id="edituser" method="post"><input type="hidden" name="action" value="updateuser">		

		<fieldset>



		<p>

		<label for="name">Name <em>*</em></label>

		<input id="name" name="name" size="25" maxlength="255" class="required" value="' . $row_user_edit['name'] . '"/>

		</p>



		<p>

		<label for="local_authority_id">Local Authority <em>*</em></label>

		<select style="width:175px;" name="local_authority_id" id="local_authority_id" value="' . $local_auth_name['la_name'] . '">			

			'.$local_authority_option.'

		</select>

		</p>



		<p>

		<label for="school_learning_est_id" >School or Learning Establishment name <em>*</em></label>

		<select style="width:175px;" name="school_learning_est_id" id="school_learning_est_id" value="' . $school_name['School'] . '">'.$tblschool_new_option.'</select>

		</p>



		<p>

		<label for="email">E-Mail <em>*</em></label>

		<input id="email" name="email" size="25" maxlength="100" class="required email" value="' . $row_user_edit['email'] . '"/>

		</p>



		<p>

		<label for="telephonenumber">Telephone Number <em>*</em></label>

		<input name="telephonenumber" id="telephonenumber" size="25" maxlength="100" class="required" value="' . $row_user_edit['telephonenumber'] . '"/>

		</p>



		<p>

		<label for="Expiry">Expiry <em>*</em></label>

		<input name="Expiry" id="Expiry" size="25" maxlength="100" class="required" value="' . $row_user_edit['Expiry'] . '"/>

		</p>



		<p>

		<input class="submit" type="submit" value="Submit"/>

		</p>

    </fieldset></form></div>';

Open in new window

0
Comment
Question by:jezskill
7 Comments
 
LVL 12

Expert Comment

by:GMGenius
ID: 33624939
Not Tested but i believe its this

http://php.net/manual/en/function.date.php 

<input name="Expiry" id="Expiry" size="25" maxlength="100" class="required" value="' . date("d/m/y", $row_user_edit['Expiry']) . '"/>
0
 
LVL 12

Expert Comment

by:GMGenius
ID: 33624945
Sorry "d/m/Y" for 4 digit year
0
 
LVL 2

Accepted Solution

by:
johnwarde earned 500 total points
ID: 33625001
<input name="Expiry" id="Expiry" size="25" maxlength="100" class="required" value="' . date("d/m/Y", strtotime($row_user_edit['Expiry'])) . '"/>

The date() function requires an integer for its second parameter, strtotime() returns the integer.

http://ie2.php.net/manual/en/function.date.php
http://ie2.php.net/manual/en/function.strtotime.php
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Expert Comment

by:avbauwel
ID: 33625350
It all depends on how you store that Expiry date.

I currently store all date/time based columns as integer values (unix timestamps) when using mySQL/PHP.

If it is stored as a unix timestamp, use the function date("d-m-Y, $row_user_edit['Expiry']) to format the timestamp loaded from database.

If stored as a mySQL datetime field, you'll need to make a function to convert MySQL's standard datetime format of YYYY-MM-DD to DD-MM-YYYY

Since you have another question open to ask abotu storing a date in format DD-MM-YYYY in a mysql date field, I will now assume that this is what you will need.

Look at these functions to swap sequence of the dates in a string:


function MysqlToPhpDate ($string) {
  $arr = explode('-', 'string) // array nog is like this: $arr[0] = '2010'; $arr[1] = '09'; $arr[2] = '08';
  $datePhp = $arr[2].'-'.$arr[1].'-'.$arr[0];
 
  return $datePhp;
}

function PhpToMysqlDate ($string) {
  $arr = explode('-', 'string) // array nog is like this: $arr[0] = '08'; $arr[1] = '09'; $arr[2] = '2010'
  $dateMysql = $arr[2].'-'.$arr[1].'-'.$arr[0];
 
  return $dateMysql;
}

You will need to do check/correction of the input field, since users can input it as a string and who knows how they will format the inputted date.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33626027
This is pretty well understood in the knowledge base of "industry best practices" -- please read this article and post back here with any specific questions.  HTH, ~Ray
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33641465
No offense meant to johnwarde, but strtotime() can also return FALSE.  That's why you might want to read the article I posted.
0
 
LVL 1

Author Comment

by:jezskill
ID: 33644617
yes of course, I tried johnwardes solution first and it worked for me. I wil review your article and comment back,

regards
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

Suggested Solutions

Title # Comments Views Activity
Creating pdf for "magazine" layout 2 24
php date - compare 4 38
object oriented programming comparison 5 52
php string masking 4 27
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
In this tutorial viewers will learn how to increase their history states in Photoshop To undo more than one history state, use Ctrl + Alt + Z, not just Ctrl + Z: View the History window by going to Window > History: The default number of history sta…
In this Micro Tutorial viewers will learn how to remove an unwanted object using Photoshop’s feature known as content-aware fill.

948 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

16 Experts available now in Live!

Get 1:1 Help Now