SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    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
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo date("d-m-Y", $yourTimestamp)
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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
    Code:
    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:
    PHP Code:
    $row['join_date'] = '2011-04-05';
    $nice_date date('d-m-Y'strtotime$row['join_date'] ));
    // 05-04-2011 
    Last edited by Cups; May 3, 2011 at 03:42. Reason: renamed vars to match sample code

  4. #4
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by smftre View Post
    echo date("d-m-Y", $yourTimestamp)
    How can I used this code? whats the used of the variable $yourTimestamp?

    Thanks

  5. #5
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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
    Code:
    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:
    PHP Code:
    $row['join_date'] = '2011-04-05';
    $nice_date date('d-m-Y'strtotime$row['join_date'] ));
    // 05-04-2011 
    How can I put that code in my code?Where should I insert the code you suggested?Thank you so much

    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>
    <script type="text/JavaScript">
     
    FUNCTION confirmDelete(){
    VAR agree=CONFIRM("Are you sure you want to delete this file?");
    IF (agree)
         RETURN TRUE ;
    ELSE
         RETURN FALSE ;
    }
    </script>
    </head>

    <body>
    <form method="post" action="test1.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' onClick='confirmDelete()';>Delete</a></td>";
        echo 
    "</tr>";
    }

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

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

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    (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.

  7. #7
    SitePoint Enthusiast webdesignhouston's Avatar
    Join Date
    Dec 2010
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rhodarose View Post
    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.
    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 Code:
    <?php
    echo date("j-n-Y",
        
    strtotime("2001-04-03")
    );
    // returns 3-4-2001 (3rd April 2001)
    ?>

  8. #8
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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.

  11. #11
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  12. #12
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,800
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    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.

  13. #13
    SitePoint Addict
    Join Date
    Jul 2010
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
  •