Change Date Format From YYYY-MM-DD to DD-MM-YYYY

Good day!

I want to know if what should I do to change the format of Date FROM YYYY-MM-DD to DD-MM-YYYY using php and also what should i put in the database as date format.

Here is my code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Machine 1 Problem</title>
</head>
<body>
<form method="post" action="machine_1.php" name="machine1">
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_name = 'db_machine1';

$query = mysql_connect("$db_host", "$db_user")or die (mysql_error());
$db = mysql_select_db("$db_name")or die (mysql_error());

$query = "SELECT * FROM tbl_machine1 ORDER BY Emp_ID ASC";
$result = mysql_query($query) or die(mysql_error());

echo "<table border='1'><tr>";
for($i = 0; $i < mysql_num_fields($result); $i++){
	//echo "<th>".mysql_field_name($result, $i)."</th>";
	echo "<th><a>.mysql_field_name($result, $i).""</a></th>";
}
echo "<th>Options</th>";
echo "</tr>";
while($row = mysql_fetch_array($result)){
	echo "<tr>";
	for($i = 0; $i < mysql_num_fields($result); $i++){
		echo "<td>". $row[$i] ."</td>";
	}
	echo "<td><a href = 'edit.php'>Edit</a> <a href = 'delete.php'>Delete</a></td>";
	echo "</tr>";
}

echo "</table>";
echo "<input type = 'button' name= 'add' value='ADD'>";

?>
</form>
</body>
</html>

Thank you

echo date(“d-m-Y”, $yourTimestamp)

If you store dates as YYYY-MM-DD in your database as a date field then that is good practice - just don’t call your field date as that is a mysql reserved word and will cause you problems when you come to things like


select * from table where YEAR(`date`) = '2011'

UNLESS you quote them with backticks as I just did above, stay away from that jiggery-pokery, call your field join_date or something like that.

Back to your question:


$row['join_date'] = '2011-04-05';
$nice_date = date('d-m-Y', strtotime( $row['join_date'] ));
// 05-04-2011

How can I used this code? whats the used of the variable $yourTimestamp?

Thanks

How can I put that code in my code?Where should I insert the code you suggested?Thank you so much


&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;

&lt;title&gt;Machine 1 Problem&lt;/title&gt;
&lt;script type="text/JavaScript"&gt;
 
FUNCTION confirmDelete(){
VAR agree=CONFIRM("Are you sure you want to delete this file?");
IF (agree)
     RETURN TRUE ;
ELSE
     RETURN FALSE ;
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form method="post" action="test1.php" name="machine1"&gt;
&lt;?php
$db_host = 'localhost';
$db_user = 'root';
$db_name = 'db_machine1';

$query = mysql_connect("$db_host", "$db_user")or die (mysql_error());
$db = mysql_select_db("$db_name")or die (mysql_error());

$query = "SELECT * FROM tbl_machine1 ORDER BY Emp_ID ASC";
$result = mysql_query($query) or die(mysql_error());

echo "&lt;table border='1'&gt;&lt;tr&gt;";
for($i = 0; $i &lt; mysql_num_fields($result); $i++){
	echo "&lt;th&gt;".mysql_field_name($result, $i)."&lt;/th&gt;";
	//echo "&lt;th&gt;&lt;a&gt;.mysql_field_name($result, $i).""&lt;/a&gt;&lt;/th&gt;";
}
echo "&lt;th&gt;Options&lt;/th&gt;";
echo "&lt;/tr&gt;";
while($row = mysql_fetch_array($result)){
	echo "&lt;tr&gt;";
	for($i = 0; $i &lt; mysql_num_fields($result); $i++){
		echo "&lt;td&gt;". $row[$i] ."&lt;/td&gt;";
		
	}
	echo "&lt;td&gt;&lt;a href = 'edit.php'&gt;Edit&lt;/a&gt; &lt;a href = 'delete.php' onClick='confirmDelete()';&gt;Delete&lt;/a&gt;&lt;/td&gt;";
	echo "&lt;/tr&gt;";
}

echo "&lt;/table&gt;";
echo "&lt;input type = 'button' name= 'add' value='ADD'&gt;";

?&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

(Inside the while loop)
for($i = 0; $i < mysql_num_fields($result); $i++){

Please dont do that. Unnecessary calls to functions will slow your script down. (mysql_num_fields will be called for every field of every row of the function!)

foreach($row AS $value) works fine…

As far as where to insert the date conversion, inside your while loop.

You should store dates in mySQL as “datetime”. You can display them in whatever way that makes sense. There is a PHP function called strtotime() that can convert strings (e.g. 2001-01-01) to PHP timestamps (e.g. 978289200). You can then feed this value to the date() function. Together, this works like this:

<?php
echo date("j-n-Y",
	strtotime("2001-04-03")
);
// returns 3-4-2001 (3rd April 2001)
?>

How can I insert that code in my php code I only want to use this on my bday field.

Here is my new code:


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/JavaScript">
function confirmDelete(){
	var agree = confirm("Are you sure you want to delete this file?");
	if(agree){
		// direct browser to delete.php
		window.location = "./delete.php";
	} else {
		// do nothing and return false
		return false ;
	}
}
</script>
</head>
 <body>
 <form name="machine1" action="page3.php" method="post">
 <table border="1">
 <tr>
 <td>Emp ID</td>
 <td>Last Name</td>
 <td>First Name</td>
 <td>Birthday</td>
 <td>Option</td>
 </tr>

 <?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db_machine1") or die(mysql_error());

 $data_p = mysql_query("SELECT * FROM tbl_machine1") or die(mysql_error());
while($info = mysql_fetch_array( $data_p ))
{
	$emp_id = $info['Emp_ID'];
	$lname = $info['Last_Name'];
	$fname = $info['First_Name'];
	$bday = $info['Birthday'];
	?>
	<tr>
	<td><?php echo $emp_id;?> </td>
    <td><?php echo $lname;?> </td>
    <td><?php echo $fname;?> </td>
    <td><?php echo $bday;?> </td>
   	<td><a href = 'edit.php'>Edit</a> <a href='delete.php?id=$emp_id' onClick='confirmDelete();'>Delete</a></td>
	</tr>
<?php
}
?>
</table>
<input type = 'button' name= 'add' value='ADD'>
 </body>
 </html>

Thank you

I tried this code:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script language="JavaScript">
	function CloseChildWindow()
	{
		window.opener.location.href="page3.php";
		self.close();
	}
</script>
</head>

<body>
<form method="post" action="add.php" name="add">
Last Name:&nbsp;<input type="text" name="Last_Name" id="Last_Name"><br/><br/>
First Name:&nbsp;<input type="text" name="First_Name" id="First_Name"><br/><br/>
Birthday:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="Birthday" id="Birthday"><br/><br/>
<input type="submit" name="save" value="SAVE" onClick="CloseChildWindow();">
<?php
//error_reporting(0);
$Lname = "";
$Fname= "";
$bday = "";


mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db_machine1") or die(mysql_error());

if(isset($_POST['save'])){
$Lname=$_POST['Last_Name'];
$Fname=$_POST['First_Name'];
$bday=$_POST['Birthday'];



$date = date('Y-m-d', strtotime($bday));
//$query = "INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday) VALUES ('$Lname','$Fname','$bday')";
//$result=mysql_query($query);

$sql = mysql_query("INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday)
VALUES('".$Lname."', '".$Fname."', '".$date."')");

}

?>
</form>
</body>
</html>

And when I insert 1990-05-25 as birthday it did not convert to 25-05-1990

Thank you

If you want it out as 25-05-1990 your date format should be ‘d-m-Y’ rather than ‘Y-m-d’

NOTE: If you’re using a mySQL Date field, the date will always be formatted in ‘Y-m-d’ when stored in the database, and you’ll need to convert it back to your chosen format when retrieving it.

Yes sir my birthday was date field in my database.is it easy for me if I used varchar as my field type in my birthday?so that I dont need to convert it?

In the practical world, you will have to convert at some point. either when the date is entered into the DB or when it’s read. My preferred method is to save as UNIX epoch time to the database. then use the date()function for output and processing.

Anyway it was already mentioned if you have a date stored in a preformated way, YYYY-MM-DD for example, you use strtotime()… that will give you its unix equivalent,
then use date(“j-n-Y”, $yourtimestamp) to get what you want.

the code, broken down for easy comprehension, is:
$dateFromDB;// put your info in this variable
$toCovert=strtotime($dateFromDB);
$toOutput(“j-n-Y”,$toCovert;// “j-n-Y”, is the pattern you want the date output as, in this case DAY-MONTH-YEAR NUMERIC.
echo $toOutput;

this can be done in one line of code, as WDHouston suggested, I just thought I would clear it up.

Thank you