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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jackgoddy123
Forum Newbie
Posts: 14
Joined: Sat Jan 11, 2014 8:10 am

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

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post 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"
}
*/
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jackgoddy123
Forum Newbie
Posts: 14
Joined: Sat Jan 11, 2014 8:10 am

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

Post 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. :(
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post by social_experiment »

social_experiment wrote:in which format do you store that dates; d/m/y ?
^ ?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jackgoddy123
Forum Newbie
Posts: 14
Joined: Sat Jan 11, 2014 8:10 am

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

Post by jackgoddy123 »

I am storing my date in "d/m/Y" format i.e: 28/01/2014
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post 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
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply