Date in dd/mm/yyyy format?

Asked

Viewed 13,793 times

5

i created a table, where I put the user record on a card for printing; therefore this table has several records (5 per page) and so in each page I access a record in the same table I did an array_push with the values -->

$sql = mysql_query("SELECT * FROM cartao LIMIT $inicial, $numreg ");
$sql_conta = mysql_query("SELECT * FROM cartao ");
$quantreg = mysql_num_rows($sql_conta); 

$Nascimento = array();
$Batismo = array();
$Comunhao = array();
$Emissao = array();

while($linha = mysql_fetch_array($sql_conta, MYSQL_ASSOC)){;
  array_push($Nascimento, $linha['Nascimento']);
  array_push($Batismo, $linha['Batismo']);
  array_push($Comunhao, $linha['Comunhao']);
  array_push($Emissao, $linha['Emissao']);

}if ($_GET['pag'] > 0 ){  
$pegaid = (int) $_GET['pag'];
$id = "$pegaid";
$soma = $id * 5 ;
}

Inside the Table I pull the data from the record that comes out in format ('yyyy/mm/dd') the table works like this:

td height="42"><a>Nascimento:</a>
                   <?php
                  echo '<b>'; 
                 if ($_GET['pag'] > 0 ){
                     print $Nascimento[0 + $soma]; 
                 }else{
                     print $Nascimento[0];
                     }
                 echo '</b>';    
                     ?>
                  </td>

I wanted to KNOW how to invert the format without giving problem in the rest of the code the other records I took for finding without importance. OBS:. All records there are dates; The record issue is an automatic record!!! Some help.

3 answers

6


Optimizing your query counter:

Your query does not count, it returns ALL records!!!
SELECT * FROM cartao
The right thing would be:
SELECT COUNT(*) AS count FROM cartao LIMIT $inicial, $numreg

My suggestion to count the records is below:

1st QUERY: SELECT SQL_CALC_FOUND_ROWS * FROM cartao LIMIT $inicial, $numreg
2nd QUERY: SELECT FOUND_ROWS()

FOUND_ROWS will return the total of records


1) using the class DateTime

$date = new DateTime( '2014-08-19' );
echo $date-> format( 'd-m-Y' );



2) As in the reference posted by @Kazzkiq

date( 'd-m-Y' , strtotime( '2014-08-19' ) );

The 2 modes produce the same result:
input: 2014-08-19
output: 19-08-2014

  • Friend your answer was the only one that worked in my case then Vlw

  • Blz. Only 1 question, what is the purpose of $sql_conta? I think your query can be optimized...

  • Sql_account pull table data already Sql is to make the pagination; the intent of the page and create several cards for printing

  • Yes only one more question if the person was born before 1970 how do I put the date ??? I used case 2

  • I updated your query to the record counter. What’s the question regarding birth before 1970?

  • Nothing else vlw the attention friend already solved!!! I thought if there was a date before 1970 it would give error; but let me forget to put birth in the BD

  • 1) To compare a DMY date you can use if( strtotime( '1969-06-24' ) <= strtotime( '1970-06-24' ) ) // anterior. 2) your $sql_conta is very wrong, your counter returns data while it should return only the total (Count(*) as Count)

  • You can explain better The context of $sql_account OBS:. The sql_account takes more data besides dates take image address and text

  • your 1ª sql should return the data you need and the second should be the counter

Show 5 more comments

2

In SQL itself you use date_format same example right below:

$sql = mysql_query("SELECT date_format(Nascimento,'%d/%m/%Y') Nascimento, Batismo, Comunhao, Emissao FROM cartao LIMIT $inicial, $numreg ");
$sql_conta = mysql_query("SELECT * FROM cartao ");
$quantreg = mysql_num_rows($sql_conta); 

$Nascimento = array();
$Batismo = array();
$Comunhao = array();
$Emissao = array();

while($linha = mysql_fetch_array($sql_conta, MYSQL_ASSOC)){;
     array_push($Nascimento, $linha['Nascimento']);
     array_push($Batismo, $linha['Batismo']);
     array_push($Comunhao, $linha['Comunhao']);
     array_push($Emissao, $linha['Emissao']);

}
if ($_GET['pag'] > 0 ){  
    $pegaid = (int) $_GET['pag'];
    $id = "$pegaid";
    $soma = $id * 5 ;
}
  • Using SQL to format an output is not the best option. Imagine your multi-language system, you will need to format the QUERY for a date in each language, or decide to change 10/06/2014 for 10 de Junho de 2014... Impractical

  • 3

    @Papacharlie disagree, it depends a lot on the context and the volume of data.

  • @gmsantos, respect but also disagree :) It is a matter of logic. If you use SQL to format the date in a specific format you will have a very fixed application, if you want to change the format inevitably you will have to change the QUERY. It will be worse if he wants to implement a cache system, in this case neither QUERY will be executed...

  • These focusing on a specific context. Take a database with a million columns and print them all in php. Directly formatting the query is much faster than iterating line by line in php to change the format

  • I’m focusing on a specific Context??? I cited 3 distinct and completely usual cases... You who focused on volume of traffic...

  • @Maria I do not know what I did wrong according to your answer, its I put up date_format invertia, more here is giving error!!! It doesn’t work, I’ve tried $sql and $sql_account and nothing anyone can help me because the push array_doesn’t work

  • @Papacharlie is only aware of not overloading layers of your application: http://stackoverflow.com/questions/24168/why-are-relational-set-based-queries-better-than-cursors

  • If you run a query to have a date D-M-Y and then need to format to Y-M-D or D-M-Y according to the user’s choice, can you tell me what your suggestion would be?

  • 1

    About the comments above: I believe that to internationalize, it would be enough to locate the %d/%m/%Y when sending the query, instead of in the output. As for the error mentioned by OP, I think it’s just a little syntax problem in the answer query.

  • The most common is to do in SQL and I do not believe I have any problem since your code for internalization dictated by you should also go through changes, sincerely not clear @Papacharlie, I can respect.

  • @Maybe I can help you ?

  • In my example just change the view and compose the HTML in the format you want without going through any change. We all respect and disagree and the result is a productive debate :)

  • @Bacco, exactly that, at best you’ll have to look for parameters. SELECT date_format(field , $formato_pt | $formato_en )... Now apply this to a cache system that does not use DB, TXT or XML... Look at the plaster

  • @Papacharlie you are complicating a subject that is defined, he wants in Brazilian format is described in the question (Date in format dd/mm/yyyy), sorry sounds silly but what you’re going to do is still in front of your code...

  • I’m not complicating, I just gave an answer that allows the system to work in any situation, including working with data that is not SQL... until

  • @Papacharlie this statement of yours is only true with parameters the same way I can work with my answer in SQL, until.

Show 11 more comments

2

Using a simple explode and implode:

$date = '2014-08-19';
echo implode('/',array_reverse(explode('-',$date)));
  • -1 This way is not very appropriate. I believe it is more readable to use the Datetime.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.