Page 1 of 1

fetch current date record of dd/mm/yyyy format in php mysql

Posted: Sun Jan 26, 2014 2:28 am
by jackgoddy123
Hello experts,

I am stuck with an query. Actually i want to fetch current date records from my database and the below code probably fetches all my current date records. But it fetches in d/m/y (26/01/14) format but i need in dd/mm/yyyy (26/01/2014) .
Below is the code which gives output as d/m/y format:

Code: Select all

<?php
ini_set( "display_errors", 0);
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$today = date('d-m-y');
$result = mysqli_query($con,"SELECT * FROM Persons WHERE DATE(startdate) = '$today'");

echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>Startdate</th>
<th>Details</th>

</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['startdate'] . "</td>";
   echo "<td>" . $row['details'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>
I tried with the above code and also tried to change the date format i.e :
$today = date('d-m-y');
to $today = date('dd-mm-yyyy');

I am not understanding what i am missing. Just need some help in this. Any help is appreciated.

Re: fetch current date record of dd/mm/yyyy format in php my

Posted: Sun Jan 26, 2014 3:59 am
by social_experiment
in which format do you store that dates; d/m/y ?

Edit
If you are storing the date in the format above you can use DATE_FORMAT() to create the output you have in mind

The snippet below might be of use to you

Code: Select all

<?php
 // example
$sql = "SELECT DATE_FORMAT(`terms`, '%e/%m/%Y') FROM `test`";	
$qry = mysql_query($sql) or die(mysql_error());
$array = mysql_fetch_array($qry);	
?>
<pre>	
	<?php var_dump($array); ?>
</pre>	

/**
	array(2) {
  [0]=>
  string(10) "14/01/2026"
  ["DATE_FORMAT(`terms`, '%e/%m/%Y')"]=>
  string(10) "14/01/2026"
}
*/

Re: fetch current date record of dd/mm/yyyy format in php my

Posted: Mon Jan 27, 2014 4:02 am
by jackgoddy123
Thanx for you reply.
I have even google and your reply is perfectly fine. But when i run the above query it results no output. I knw that for "Y" i can get "2014" but when i use it in query its not giving any output. Inside my database i have records with my current date. But unfortunately it fetches no output. :(

Re: fetch current date record of dd/mm/yyyy format in php my

Posted: Mon Jan 27, 2014 2:16 pm
by social_experiment
social_experiment wrote:in which format do you store that dates; d/m/y ?
^ ?

Re: fetch current date record of dd/mm/yyyy format in php my

Posted: Tue Jan 28, 2014 12:43 am
by jackgoddy123
I am storing my date in "d/m/Y" format i.e: 28/01/2014

Re: fetch current date record of dd/mm/yyyy format in php my

Posted: Tue Jan 28, 2014 1:14 am
by social_experiment
jackgoddy123 wrote:$today = date('d-m-y');
to $today = date('dd-mm-yyyy');
have you tried

Code: Select all

<?php
 $today = date('d/m/Y');  // returns 01/28/2014
?>