Home php Change Date format in PHP

Change Date format in PHP

Author

Date

Category

I have two fields in the database with Date1 dates, Date1 and in them the date is displayed as follows: 2016-09-25. I want to do this: 25-09-2016.
I tried to change the format on: D-M-Y, but displays the date 0000-00-00

Help please.

in a file that displays data from the database such:

$ rezult = mysql_query ("select * from klienti");

in the file that adds a request code such:

& lt;! doctype HTML & GT;
& lt; html & gt;
& lt; Head & gt;
& lt; script Type = "text / javascript" src = "calendar / tcal.js" & gt; & lt; / script & gt; & lt;! - // Calendar - & gt;
& lt; link rel = "StyleSheet" Type = "text / css" href = "calendar / tcal.css" / & gt; & lt;! - // Calendar - & gt;
& lt; Meta http-equiv = "content-type" content = "text / html; charset = utf-8" / & gt;
& lt; Title & gt; adding a client & lt; / title & gt;
& lt; script src = "https://ajax.googleapis.com/ajax/libs/jquery/1/JQuery.min.js" & gt; & lt; / script & gt;
& lt; script type = "text / javascript" & gt;
$ ("Document"). READY (FUNCTION () {
$ ("Form"). Submit (Event) {
  event.preventDefault (); // !!!
  var dannie = $ ("Form"). serialize ();
  $.ajax ({
    URL: 'INSERT.PHP',
    Type: 'Post',
    Data: Dannie,
    SUCCESS: FUNCTION (DATA) {
      if (DATA) {
        Alert ("Successful Added");
           }
      ELSE {
      Alert ("Error");
         }
        }
});
});
});
& lt; / script & gt;
& lt; / Head & gt;
& lt; Body & gt;
& lt; Form & gt;
client & lt; br / & gt;
& lt; Input Type = "text" name = "klient" / & gt; & lt; br / & gt;
OP_FORM & LT; BR / & GT;
& lt; Textarea Name = "OP_FORMA" & GT; & LT; / Textarea & GT; & lt; br / & gt; & lt;! - cols = "10" rows = "10" - & gt;
NAME & LT; BR / & GT;
& lt; Input Type = "Text" name = "Naimen" / & gt; & lt; br / & gt; & lt; br / & gt;
branch & lt; br / & gt;
& lt; Input Type = "text" name = "otrasl" / & gt; & lt; br / & gt; & lt; br / & gt;
Telefon & lt; br / & gt;
& lt; Input Type = "text" name = "Telefon" / & gt; & lt; br / & gt; & lt; br / & gt;
Email & lt; br / & gt;
& lt; Input Type = "text" name = "email" / & gt; & lt; br / & gt; & lt; br / & gt;
Contact person & lt; br / & gt;
& lt; Input Type = "text" name = "kont_lico" / & gt; & lt; br / & gt; & lt; br / & gt;
Position & lt; br / & gt;
& lt; Input Type = "text" name = "Dolznost" / & gt; & lt; br / & gt; & lt; br / & gt;
& lt; Input Type = "Hidden" name = "date" value = "& lt;? php echo date ('d-m-y');? & gt;" / & gt;
& lt; input type = "hidden" name = "time" value = "& lt;? PHP ECHO DATE ('H: I: S');? & GT;" / & gt;
& lt; br / & gt;
& lt; Input Type = "Text" name = "Date1" class = "TCAL" VALUE = "" / & GT;
& lt; br / & gt;
& lt; Input Type = "submit" id = "Send" value = "Add" / & gt;
& lt; Input Type = "Button" name = "reset_form" value = "Clear the form" OnClick = "this.form.reset ();" & gt;
& lt; / form & gt;
& lt; / body & gt;
& lt; / html & gt;

In the file that is responsible for insertion in the database

& lt;? 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;
 ? & gt;

Answer 1, Authority 100%

DATE data type is used for values ​​containing information about
date. MySQL extracts and outputs Date values ​​in the ‘yyyy-mm-dd’ format.

You will not be able to change the storage format on another in the database, this is a MYSQL standard.

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

datetime :: createfromformat ('ym-d', '2015-05-24') - & gt; format ('Dm -Y ');

Answer 2, Authority 50%

date_format (date_create ($ date), 'd-m-y');

Answer 3, Authority 50%

You can try StrTotime ($ Date)

date (StrTotime ($ Date), 'D-M-Y');

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


Answer 4

  1. mysql – outdated extension. Use MySQLI
    https://habrahabr.ru/post/141127/
    http://phpclub.ru/detail/article/mysqli

  2. Fuck SQL injections, it is impossible to give directly to the database that it came to the $ _Post (Values ​​”array. $ _post [‘klient’].” ‘,’. $ _Post [‘OP_FORMA’])
    https://habrahabr.ru/post/148151/
    https://habrahabr.ru/post/148701/

  3. Try using a normal IDE for writing a code that will teach good code formatting, except for other advantages (for example PHPSTORM, it can automatically format the CTRL + Alt + L key combination code)

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

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions