Jump to content

How to change date format


arjanvr

Recommended Posts

How do I change the date format so it reads Day-month-Year (example 13-05-2013) in the output and allows me to put it in that format in the input form.. I read a couple of pages how to do it but that did not work so I hope someone can either fix it for me or tell me exactly what to change so i can learn from that..

 

much appriciated

 

The input and edit form

<?php
        /*
                Staat de gebruiker toe om nieuwe records toe te voegen te bewerken
        */

        // connect to the database
        include("connect-db.php");

        // Maakt nieuw/edit record formulier
        function renderForm($klantnummer = '', $bedrijfsnaam ='', $adres ='', $postcode ='', $plaats ='', $londerhoud ='', $aantal ='', $vonderhoud='', $error = '', $id = '')
        { ?>
                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                <html>
                        <head>  
                                <title>
                                        <?php if ($id != '') { echo "Bewerk onderhoud"; } else { echo "Nieuw onderhoud"; } ?>
                                </title>
                                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
                        </head>
                        <body>
                                <h1><?php if ($id != '') { echo "Bewerk onderhoud"; } else { echo "Nieuw onderhoud"; } ?></h1>
                                <?php if ($error != '') {
                                        echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
                                                . "</div>";
                                } ?>
                                
                                <form action="" method="post">
                                <div>
                                        <?php if ($id != '') { ?>
                                                <input type="hidden" name="id" value="<?php echo $id; ?>" />
                                                <p>ID: <?php echo $id; ?></p>
                                        <?php } ?>
                                        
                                        <strong>Klantnummer:</strong> <input type="text" name="klantnummer"
                                                value="<?php echo $klantnummer; ?>"/><br/>
                                        <strong>Bedrijfsnaam:</strong> <input type="text" name="bedrijfsnaam"
                                                value="<?php echo $bedrijfsnaam; ?>"/><br/>
                                        <strong>Adres:</strong> <input type="text" name="adres"
                                                value="<?php echo $adres; ?>"/><br/>
                                        <strong>Postcode:</strong> <input type="text" name="postcode"
                                                value="<?php echo $postcode; ?>"/><br/>
                                        <strong>Plaats:</strong> <input type="text" name="plaats"
                                                value="<?php echo $plaats; ?>"/><br/>
                                        <strong>Laatste onderhoud:<br><small>jaar-maand-dag</small></strong> <input type="text" name="londerhoud"
                                                value="<?php echo $londerhoud; ?>"/><br/>
                                        <strong>Aantal apparaten:</strong> <input type="text" name="aantal"
                                                value="<?php echo $aantal; ?>"/><br/>
                                        <strong>Volgende onderhoud:<br><small>jaar-maand-dag</small></strong> <input type="text" name="vonderhoud"
                                                value="<?php echo $vonderhoud; ?>"/><p/>
                                        <input type="submit" name="submit" value="Submit" /><p>
                                        <a href="index.html">Terug naar het overzicht</a>
                                </div>
                                </form>
                        </body>
                </html>
                
        <?php }



        /*

           BEWERK RECORD

        */
        // Wanneer de 'id' variabel is ingesteld in de URL, weten we dat we een record moeten aanpassen
        if (isset($_GET['id']))
        {
                // Wanneer de submitknop word ingedrukt word het formulier verwerkt
                if (isset($_POST['submit']))
                {
                        // Zorgt ervoor dat de 'id' geldig is
                        if (is_numeric($_POST['id']))
                        {
                                // verkrijg variabelen van URL/formulier
                                $id = $_POST['id'];
                                $klantnummer = htmlentities($_POST['klantnummer'], ENT_QUOTES);
                                $bedrijfsnaam = htmlentities($_POST['bedrijfsnaam'], ENT_QUOTES);
                                $adres = htmlentities($_POST['adres'], ENT_QUOTES);
                                $postcode = htmlentities($_POST['postcode'], ENT_QUOTES);
                                $plaats = htmlentities($_POST['plaats'], ENT_QUOTES);
                                $londerhoud = htmlentities($_POST['londerhoud'], ENT_QUOTES);
                                $aantal = htmlentities($_POST['aantal'], ENT_QUOTES);
                                $vonderhoud = htmlentities($_POST['vonderhoud'], ENT_QUOTES);
                                
                                {
                                        // update de database
                                        if ($stmt = $mysqli->prepare("UPDATE klanten SET klantnummer = ?, bedrijfsnaam = ?, adres = ?, postcode = ?, plaats = ?, londerhoud = ?, aantal = ?, vonderhoud = ?
                                                WHERE id=?"))
                                        {
                                                $stmt->bind_param("ssssssssi", $klantnummer, $bedrijfsnaam, $adres, $postcode, $plaats, $londerhoud, $aantal, $vonderhoud, $id);
                                                $stmt->execute();
                                                $stmt->close();
                                        }
                                        // toont foutmelding indien nodig
                                        else
                                        {
                                                echo "ERROR: could not prepare SQL statement.";
                                        }
                                        
                                        // redirect na submit van formulier
                                        header("Location: view.php");
                                }
                        }
                        // bij ongeldige 'id' komt een foutmelding
                        else
                        {
                                echo "Error!";
                        }
                }
                // Indien het formulier niet verstuurd is word de database en het formulier weergegeven
                else
                {
                        // zorgt dat de 'id' geldig is
                        if (is_numeric($_GET['id']) && $_GET['id'] > 0)
                        {
                                // verkrijg 'id' van URL
                                $id = $_GET['id'];
                                
                                // verkrijg de records uit de database
                                if($stmt = $mysqli->prepare("SELECT * FROM klanten WHERE id=?"))
                                {
                                        $stmt->bind_param("i", $id);
                                        $stmt->execute();
                                        
                                        $stmt->bind_result($id, $klantnummer, $bedrijfsnaam, $adres, $postcode, $plaats, $londerhoud, $aantal, $vonderhoud);
                                        $stmt->fetch();
                                        
                                        // toon het formulier
                                        renderForm($klantnummer, $bedrijfsnaam, $adres, $postcode, $plaats, $londerhoud, $aantal, $vonderhoud, NULL, $id);
                                        
                                        $stmt->close();
                                }
                                // toon een error wanneer de query een error heeft
                                else
                                {
                                        echo "Error: could not prepare SQL statement";
                                }
                        }
                        // wanneer het 'id' ongeldig is word de gebruiker naar view.php doorgestuurd
                        else
                        {
                                header("Location: view.php");
                        }
                }
        }



        /*

           NIEUW RECORD

        */
        // wanneer geen 'id' is ingesteld volgt een nieuwe invoer
        else
        {
                // proces het formlier na submit
                if (isset($_POST['submit']))
                {
                        // verkrijg formulierdata
                        $klantnummer = htmlentities($_POST['klantnummer'], ENT_QUOTES);
                        $bedrijfsnaam = htmlentities($_POST['bedrijfsnaam'], ENT_QUOTES);
                        $adres = htmlentities($_POST['adres'], ENT_QUOTES);
                        $postcode = htmlentities($_POST['postcode'], ENT_QUOTES);
                        $plaats = htmlentities($_POST['plaats'], ENT_QUOTES);
                        $londerhoud = htmlentities($_POST['londerhoud'], ENT_QUOTES);
                        $aantal = htmlentities($_POST['aantal'], ENT_QUOTES);
                        $vonderhoud = htmlentities($_POST['vonderhoud'], ENT_QUOTES);
                        
                        {
                                // insert the new record into the database
                                if ($stmt = $mysqli->prepare("INSERT klanten (klantnummer, bedrijfsnaam, adres, postcode, plaats, londerhoud, aantal, vonderhoud) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"))
                                {
                                        $stmt->bind_param("ssssssss", $klantnummer, $bedrijfsnaam, $adres, $postcode, $plaats, $londerhoud, $aantal, $vonderhoud);
                                        $stmt->execute();
                                        $stmt->close();
                                }
                                // Toon een foutmelding indien nodig
                                else
                                {
                                        echo "ERROR: Could not prepare SQL statement.";
                                }
                                
                                // stuur gebruiker door
                                header("Location: view.php");
                        }
                        
                }
                // wanneer geen formulier is ingediend word de database weergegeven
                else
                {
                        renderForm();
                }
        }
        
        // sluit mysqli verbinding
        $mysqli->close();
?>

The output

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
        <head>  
                <title>Overzicht facturen</title>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>
                
                <h1>Facturen overzicht</h1>
                
                <p><b>Bekijk alles</b> | <a href="facturen-pagina.php">Bekijk per 10</a></p>
                
                <?php
                        // connect to the database
                        include('connect-db.php');
                        

                        
                        // get the records from the database
                        if ($result = $mysqli->query("SELECT * FROM facturen ORDER BY bedrijfsnaam"))
                        {
                                // display records if there are records to display
                                if ($result->num_rows > 0)
                                {
                                        // display records in a table
                                        echo "<table border='1' cellpadding='10'>";
                                        
                                        // set table headers
                                        echo "<tr><th>id</th><th>Bedrijfs(naam)</th><th>factuurbedrag</th><th>Vervaldatum</th><th>Voldaan<br><small>ja/nee</small></th><th>Opmerkingen</th><th></th><th></th></tr>";
                                        
                                        while ($row = $result->fetch_object())
                                        {
                                                // set up a row for each record
                                                echo "<tr>";
                                                echo "<td>" . $row->id . "</td>";
                                                echo "<td>" . $row->bedrijfsnaam . "</td>";
                                                echo "<td>" . $row->factuurbedrag . "</td>";
                                                echo "<td>" . $row->vervaldatum . "</td>";
                                                echo "<td>" . $row->voldaan . "</td>";
                                                echo "<td>" . $row->opmerkingen . "</td>";
                                                echo "<td><a href='facturen-nw.php?id=" . $row->id . "'>Bewerken</a></td>";
                                                echo "<td><a href='del.php?id=" . $row->id . "'>Verwijderen</a></td>";
                                                echo "</tr>";
                                        }
                                        
                                        echo "</table>";
                                }
                                // if there are no records in the database, display an alert message
                                else
                                {
                                        echo "No results to display!";
                                }
                        }
                        // show an error if there is an issue with the database query
                        else
                        {
                                echo "Error: " . $mysqli->error;
                        }
                        
                        // close database connection
                        $mysqli->close();
                
                ?>
                
                <a href="facturen-nw.php">Voeg nieuwe factuur toe</a> -- <a href="index.html">Terug naar Administratie</a>
        </body>
</html>
Link to comment
Share on other sites

What/where is your date field? How is the date stored in your database?

 

Functions you'll want to look at are date and/or strtotime (depening on how you're storing the date).

 

These are date fields and they are stored in the database as type Date in edit/add form.

<strong>Laatste onderhoud:<br><small>jaar-maand-dag</small></strong> <input type="text" name="londerhoud"
value="<?php echo $londerhoud; ?>"/><br/>
<strong>Volgende onderhoud:<br><small>jaar-maand-dag</small></strong> <input type="text" name="vonderhoud"
value="<?php echo $vonderhoud; ?>"/><p/>

$londerhoud = htmlentities($_POST['londerhoud'], ENT_QUOTES);
$vonderhoud = htmlentities($_POST['vonderhoud'], ENT_QUOTES);

Although now I see mixed up the 2 forms I have so the bottom field is from a different form but the they are the same way build so if i know how to do one of the 2 I can do the other myself..

 

This is also a date which needs to be changed

echo "<td>" . $row->vervaldatum . "</td>";
Edited by arjanvr
Link to comment
Share on other sites

 

Select date in format day-month-Year (01-01-2001) from a date column.

SELECT DATE_FORMAT(date_column,'%d-%m-%Y') AS date FROM table

 

Does it matter where I place that on the page or does it need to be in the line where I actually print the date. Does that work for both the input form to put into the script aswell as the script that displays it on the page or do I only need it to put it correctly into the database and I can simply keep the echo as is?

Link to comment
Share on other sites

Does it matter where I place that on the page or does it need to be in the line where I actually print the date. Does that work for both the input form to put into the script aswell as the script that displays it on the page or do I only need it to put it correctly into the database and I can simply keep the echo as is?

 

It's a MySQL function, so it goes in the query where you get the date fields from the database. More information can be found here:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.