Jump to content

How to change format of date from YYYY-MM-DD to DD-MM-YYYY in PHP?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
Good day!

I encountered error in saving data data in my database.

First, I have code for uploading/importing .xml file to database.

in my .xml file the date is : 10/1/2011 but when it was display in webpage the result is :
2011-01-10T00:00:00.000

and I used
$date = substr($date,0,-13);
to remove the last part.

and now I used this code:

$date = strtotime($date); 

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

to change the format of date..the good thing is it was display as 01-10-2011 when I run that code. But it did not save in database.

Here is my whole code:

<?php

$data = array();



$con = mysql_connect("localhost", "root","");

if (!$con) { 

  die(mysql_error());

}

$db = mysql_select_db("db_upload", $con);

if (!$db) { 

  die(mysql_error());

}


$sql = "select * from employee";

$result =  mysql_query($sql, $con);

if (!$result) {

    die(mysql_error());

}

$total = mysql_num_rows($result);

if ($total > 0) {

    $sql = "delete from employee";

    $result =  mysql_query($sql, $con);

    if (!$result) {

        die(mysql_error());

    }

}

  

function add_employee($emp, $employee, $last, $mi, $date, $time)

  {

      global $data; 

      

      $con = mysql_connect("localhost", "root","");

      if (!$con){ die(mysql_error());}

      $db = mysql_select_db("db_upload", $con);

      if (!$db) { 

          die(mysql_error());

      }


      $emp = $emp;

      $employee = $employee;

      $last = $last;

      $mi = $mi;

   

      $date = substr($date,0,-13);

      $time = substr($time,11,-4);

      $date = strtotime($date); 

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

      

      $time = strftime('%I:%M %p', strtotime($time));



           

      $sql = "INSERT INTO employee (EMP_NO, Name, last, mi, date, time) VALUES ('$emp', '$employee', '$last', '$mi', '$date', '$time')";

      mysql_query($sql, $con);

      

    

      $data []= array('EMP_NO' => $emp, 'Name' => $employee, 'last' => $last, 'mi' => $mi, 'date' => $date, 'time' => $time);

      

  }

  

  if ( $_FILES['file']['tmp_name'] )

  {

      $dom = DOMDocument::load( $_FILES['file']['tmp_name'] );


      $rows = $dom->getElementsByTagName( 'Row' );

      global $last_row;

      $last_row = false;

      $first_row = true;

      foreach ($rows as $row)

      {

          if ( !$first_row )

          {

             

              $emp = "";

              $employee = "";

              $last = "";

              $mi = "";

              $date = "";

              $time = "";

              

              

              $index = 1;

              $cells = $row->getElementsByTagName( 'Cell' );

          

              foreach( $cells as $cell )

              { 

                  $ind = $cell->getAttribute( 'Index' );

                  if ( $ind != null ) $index = $ind;

                  

                  if ( $index == 1 ) $emp = $cell->nodeValue;

                  if ( $index == 2 ) $employee = $cell->nodeValue;

                  if ( $index == 3 ) $last = $cell->nodeValue;

                  if ( $index == 4 ) $mi = $cell->nodeValue;

                  if ( $index == 5 ) $date = $cell->nodeValue;

                  if ( $index == 6 ) $time = $cell->nodeValue;

                  $index += 1;

              }

         

              if ($emp=='' and $employee=='' and $last=='' and $mi=='' and $date=='' and $time=='') {

                    $last_row = true;

              }      

              else {

                

                    add_employee($emp, $employee, $last, $mi, $date, $time);

              }      

          }

          if ($last_row==true) {

              $first_row = true;

          }     

          else {

              $first_row = false;

          }

       

      }

  }

  ?>

  

  <html>

  <body>

  <table>

  <tr>

      <th>Employee Attendance</th>

  </tr>


  <?php foreach( $data as $row ) { ?>

  <tr>

  <td><?php echo( $row['EMP_NO'] ); ?></td> 

  <td><?php echo( $row['Name'] ); ?></td>

  <td><?php echo( $row['last'] ); ?></td>

  <td><?php echo( $row['mi'] ); ?></td>

  <td><?php echo( $row['date'] ); ?></td>

  <td><?php echo( $row['time'] ); ?></td>

  </tr>

  <?php } ?>

  </table>

  </body>

 </html>


Thank you

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What is the data type of the field in your database? If it's a date or datetime field, then it has no concept of format. If it's a varchar, then you should really convert it to a date or datetime field.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
I think you shouldn't care about having a specific storage format. Just use the database default, then present the database data in your desired format with your code with strtotime() and date(). I think that is a good practice in most situations. Store data as pure as possible and handle the formats on input and output instead. In many systems, users from different parts of the world want a date formatted differently, so if you store in a common format, then you can present it to each user in a way that user wants it.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
Thank you...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users