I have two fields in the database with dates date, date1 and the date in them is now displayed like this: 2016-09-25. I want to do this: 25-09-2016. Tried to change the format to: dmY, but displays the date on the site.

Help me please.

In the file that displays the data from the database, the query is:

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

In the file that adds the request, the code is:

 <!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 ('dm-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; ?> 

    4 answers 4

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

    You can not change the storage format in the database to any other way - this is the MySQL standard.

    To format the output, use the DateTime class, which allows you to conveniently work with dates.

     DateTime::createFromFormat('Ym-d', '2015-05-24')->format('dm-Y'); 
    • Can you please tell me how to use the class? I'm still new and do not really understand how to do it. Now the date is displayed like this: <td> <? Php echo $ row ['date']?> </ Td> - roman
    • one
      @roman <td><?php echo DateTime::createFromFormat('Ymd',$row['date'])->format('dmY');?></td> - Firepro
     date_format(date_create($date), 'dm-Y'); 
    • Please tell me where in the code I insert it? - roman
    • one
      where you need to display the date in the right format - ArchDemon
    • Now it’s displayed like this: <td> <? Php echo $ row ['date1']?> </ Td> I’m trying to do this: <td> <? Php echo $ row date_create ['date1'], 'dmY' ?> </ td> But it displays an error - roman

    you can strtotime($date)

     date(strtotime($date),'dm-Y'); 

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

      1. mysql is an obsolete extension. Use mysqli https://habrahabr.ru/post/141127/ http://phpclub.ru/detail/article/mysqli

      2. Be wary of SQL injection, you cannot give directly to the database what came in the $ _POST array (VALUES ('". $ _POST [' klient ']."', '". $ _POST [' op_forma ']) https: // habrahabr. com / post / 148151 / https://habrahabr.ru/post/148701/

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

      4. As for the answer to the question, as was written above, the DateTime class is of help. http://ru2.php.net/manual/ru/book.datetime.php