Log in

View Full Version : Date Format From YYYY-MM-DD To DD-MM-YYYY



rhodarose
05-03-2011, 09:14 AM
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

bluewalrus
05-03-2011, 09:39 PM
This should work


$date = "2011-05-03";
$date = substr($date, 8, 2) . '-' . substr($date, 5, 2) . '-' . substr($date, 0, 4);


There probably is a convert function in your SQL you can use as well. You can do this a number of other ways also...

fastsol1
05-03-2011, 11:40 PM
Well you don't give us any date info to look at in your code but this will work, you just need to change the $sp_end to your var from the db. You can just use the normal date format in the db to store the date.

$enddate = date_create("$sp_end");
$the_end = date_format($enddate, 'm-d-Y');

rhodarose
05-04-2011, 04:57 AM
Well you don't give us any date info to look at in your code but this will work, you just need to change the $sp_end to your var from the db. You can just use the normal date format in the db to store the date.

$enddate = date_create("$sp_end");
$the_end = date_format($enddate, 'm-d-Y');

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

fastsol1
05-04-2011, 11:55 AM
<?php
$emp_id = $info['Emp_ID'];
$lname = $info['Last_Name'];
$fname = $info['First_Name'];
$bday = $info['Birthday'];
$birth = date_create("$bday");
$birthday = date_format($birth, 'm-d-Y');
?><tr>
<td><?php echo $emp_id;?> </td>
<td><?php echo $lname;?> </td>
<td><?php echo $fname;?> </td>
<td><?php echo $birthday;?> </td>

rhodarose
05-04-2011, 01:10 PM
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

Beverleyh
05-04-2011, 01:22 PM
Look slike you havent reformatted the date here;


$date = date('Y-m-d', strtotime($bday));

rhodarose
05-05-2011, 04:57 AM
How can I reformat it?I need to know it now maam..

Thank you so much

Beverleyh
05-05-2011, 10:00 AM
You should just need to reverse the 'Y-m-d' part.

Other date format options can be found here: http://php.net/manual/en/function.time.php

rhodarose
05-05-2011, 01:06 PM
Thank you