Maiky Jolling
Maiky Jolling

Reputation: 11

Convert date to dutch format

I want to input the date in MySQL in Dutch format. now he is giving me the standard format. Do i need to change it in the output lists or in the insert files?

This is my insert code:

<?php
session_start();


include '../dbh.php';

$costname = $_POST['costname'];
$category = $_POST['category'];
$subcategory = $_POST['subcategory'];
$price = $_POST['price'];
$info = $_POST['info'];
$costdate = $_POST['costdate']->format("d-m-Y H:i:s");
$iduser = $_SESSION['id'];

if(empty($costname)) {
  header("Location: ../dashboard.php?error=emptycostname");
  exit();
}

if(empty($category)) {
  header("Location: ../dashboard.php?error=emptycatergory");
  exit();
}

if(empty($subcategory)) {
  header("Location: ../dashboard.php?error=emptysubcatergory");
  exit();
}

if(empty($price)) {
  header("Location: ../dashboard.php?error=emptyprice");
  exit();
}

if(empty($info)) {
  header("Location: ../dashboard.php?error=emptyinfo");
  exit();
}

 else {
  $sql = "INSERT INTO costs (costname, category, subcategory, price, info, userid, costdate)
  VALUES ('$costname', '$category', '$subcategory', '$price', '$info', '$iduser', '$costdate')";

  $result = mysqli_query($conn, $sql);

  header("Location: ../dashboard.php");

}

Now i need to display it on an list, I have made this code to get it from the db:

<?php
include('includes/sessionstart.php');
include 'dbh.php';
if (isset($_SESSION['id'])) { ?>


    <?php include 'includes/header.php';




    // costslist define mysql
    $sql_costs = "SELECT * FROM costs WHERE userid='".$_SESSION['id']."'";
    $result_costs = mysqli_query($conn, $sql_costs);




    $costname = $row_costs['costname'];





    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="header">
                            <h4 class="title">Wonen</h4>
                            <p class="category">Kosten gespendeerd aan uw woning.</p>
                        </div>
                        <div class="content table-responsive table-full-width">
                            <table class="table table-hover table-striped">
                                <thead>
                                                                    <th>Subcategorie</th>
                                                                    <th>Kostnaam</th>
                                                                    <th>Kostprijs</th>
                                                                    <th>Extra info</th>
                                                                    <th>Datum toegevoegd</th>

                                </thead>
                                <tbody>

                                                                            <?php
                                                                            while($row = $result_costs->fetch_assoc()) { ?>
                                                                                <tr>
                                                                                    <td><?php echo $row['subcategory']; ?></td>
                                                                                    <td><?php echo $row['costname']; ?></td>
                                                                                    <td><?php echo $row['price']; ?></td>
                                                                                    <td><?php echo $row['info']; ?></td>
                                                                                    <td><?php echo $row['costdate']; ?></td>

                                                                                    </tr>
                                                                            <?php } ?>
                                </tbody>
                            </table>

                        </div>
                    </div>
                </div>

Upvotes: 1

Views: 867

Answers (1)

Clay
Clay

Reputation: 4760

You can use MySQL's DATE_FORMAT function to format it however you want. Here is how you can update the select query to format it:

$sql_costs = "SELECT *, DATE_FORMAT(costdate, '%d-%m-%Y %H:%i:%s') AS costdate FROM costs WHERE userid='".$_SESSION['id']."'";

Read more about the DATE_FORMAT function here:

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

Upvotes: 1

Related Questions