Results 1 to 10 of 10

Thread: Date Format From YYYY-MM-DD To DD-MM-YYYY

  1. #1
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default 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:
    PHP 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

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    This should work

    PHP Code:
    $date "2011-05-03";
    $date substr($date82) . '-' substr($date52) . '-' substr($date04); 
    There probably is a convert function in your SQL you can use as well. You can do this a number of other ways also...
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    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.
    PHP Code:
    $enddate date_create("$sp_end");
            
    $the_end date_format($enddate'm-d-Y'); 

  4. #4
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by fastsol1 View Post
    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.
    PHP Code:
    $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:
    PHP 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

  5. #5
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    PHP Code:
    <?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>

  6. #6
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    I tried this code:
    PHP 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
    Last edited by rhodarose; 05-04-2011 at 01:16 PM.

  7. #7
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Look slike you havent reformatted the date here;

    PHP Code:
    $date date('Y-m-d'strtotime($bday)); 
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  8. #8
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    How can I reformat it?I need to know it now maam..

    Thank you so much

  9. #9
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    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
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  10. #10
    Join Date
    Jul 2010
    Posts
    228
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default

    Thank you

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •