Change the date format in php

I have two fields in the database with dates date, date1 and in them the date is now displayed as follows: 2016-09-25.I want to do this: 25-09-2016. I tried to change the format to: d-m-Y ,but it outputs the date 0000-00-00

Help please.

In the file that outputs data from the database, the request is as follows:

$rezult=mysql_query("SELECT * FROM klienti");

In the file that adds the request the code is as follows:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="calendar/tcal.js"></script> <!-- //календарь -->
<link rel="stylesheet" type="text/css" href="calendar/tcal.css" /> <!-- //календарь -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Добавление клиента</title>

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$ ("form").submit(function(event){
    event.preventDefault(); // !!!
    var dannie = $("form").serialize();
    $.ajax({
        url:'insert.php',
        type: 'POST',
        data: dannie,
        success: function (data) {
            if (data) {
                alert("Успешно добавлена") ;
                      } 
            else {          
            alert("Ошибка");
                 }
                }
});
});
});
</script>
</head>
<body>
<form>
клиент<br/>
<input type="text" name="klient" /><br/>

ОП_форма <br/>
<textarea name="op_forma" ></textarea> <br/> <!-- cols="10" rows="10" -->
Наимен <br/>
<input type="text" name="naimen"  /><br/><br/>
отрасль <br/>
<input type="text" name="otrasl"  /><br/><br/>
telefon <br/>
<input type="text" name="telefon"  /><br/><br/>
email <br/>
<input type="text" name="email"  /><br/><br/>
Контактное лицо <br/>
<input type="text" name="kont_lico"  /><br/><br/>
Должность <br/>
<input type="text" name="dolznost"  /><br/><br/>
<input type="hidden" name="date" value="<?php echo date ('d-m-Y');?>" />
<input type="hidden" name="time" value="<?php echo date ('H:i:s');?>" />
<br/>
<input type="text" name="date1" class="tcal"  value="" />
<br/>
<input type="submit"  id="send"  value="Добавить" />
<input type="button" name="reset_form" value="Очистить форму" onclick="this.form.reset();">
</form>
</body>
</html>

In the file responsible for inserting into the database

<?php 
$connection=mysql_connect("localhost","reklama","reklama") ;
mysql_select_db('reklama');
mysql_set_charset("utf8");
$ret=true;

mysql_query(" INSERT INTO `klienti` (klient,op_forma,naimen,otrasl,telefon,email,kont_lico,dolznost,date,date1,time)
    VALUES ('". $_POST ['klient'] ."' , '" .  $_POST ['op_forma']  ."', '" .  $_POST ['naimen']  ."', '" .  $_POST ['otrasl']  ."', '" .  $_POST ['telefon']  ."', '" .  $_POST ['email']  ."', '" .  $_POST ['kont_lico']  ."', '" .  $_POST ['dolznost']  ."', '" .  $_POST ['date']  ."', '" .  $_POST ['date1']  ."', '" .  $_POST ['time']  ."')") or $ret=false;
echo $ret;
 ?>
Author: roman, 2016-09-27

4 answers

The DATE data type is used for values containing date information. MySQL extracts and outputs DATE values in the format 'YYYY-MM-DD'.

You will not be able to change the storage format in the database to another one in any way - this is the MySQL standard.

To format the output, use the DateTime class, which makes it convenient to work with dates.

DateTime::createFromFormat('Y-m-d', '2015-05-24')->format('d-m-Y');
 2
Author: Firepro, 2016-09-27 22:05:10
date_format(date_create($date), 'd-m-Y');
 1
Author: ArchDemon, 2016-09-27 19:47:57

You can try strtotime($date)

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

Http://sandbox.onlinephpfunctions.com/code/ffb08eac54ea6208556727e9b4d27f54647bbc15

 1
Author: Naumov, 2016-09-27 20:51:48
  1. Mysql is a deprecated extension. Use mysqli https://habrahabr.ru/post/141127/ http://phpclub.ru/detail/article/mysqli

  2. Beware of SQL injections, you can not directly give to the database what came in the array $_POST (VALUES ( '" . $_POST ['klient'] ."' , '" . $_POST ['op_forma']) https://habrahabr.ru/post/148151/ https://habrahabr.ru/post/148701/

  3. Try using a normal IDE for writing code that teaches good code formatting, in addition to other advantages (for example, PhpStorm, it can automatically format code using the keyboard shortcut ctrl+alt+L)

  4. As for the answer to the question, as we wrote above, the DateTime class helps. http://ru2.php.net/manual/ru/book.datetime.php

 0
Author: Dmitry G, 2016-09-28 01:22:33