• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1428
  • Last Modified:

how to format dates using php + mysql

Hi,
I have a mysql database, and using php, i want to format the date in european format, which is dd/mm/yyyy

the dates are being inputed to the database using the curdate function, the current output of a date is

2007-11-27 00:00:00


i need it to be 27/11/2007

thanks
0
markmchugh
Asked:
markmchugh
  • 2
  • 2
  • 2
  • +2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
 
markmchughAuthor Commented:
can i do it in php?
0
 
Muhammad WasifCommented:
Why you want to do it in PHP? Anyway take a look at this http://www.php.net/manual/en/ref.datetime.php
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nplibCommented:
Change the datetime field to a date field, this will eliminate the 00:00:00 portion of the field.

Then I would format it using PHP, it's slightly faster.

$sql = "select date_field from table;";
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs)) {
      $formated_date = date("d/m/Y", strtotime($row['date_field']);
      echo $formated_date;
}
0
 
Muhammad WasifCommented:
@nplib
Is it faster to format the date in PHP rather than MySQL? How?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>can i do it in php?

yes. strtotime() will get the time value from the string yyyy-mm-dd.
then, with the date() function, you can format it to the format you want

0
 
nizsmoDeveloperCommented:
strtotime() as angellll said:
<?php
	$date = "2007-11-27 00:00:00";
	
	echo date("d-m-Y",strtotime($date));
?>

Open in new window

0
 
nplibCommented:
@wasifg
just the way sql severs process commands.

I'm not to sure why it is, but I have a table that has 3000+ records, and when I do it with date_format() it takes 0.05 seconds to return all, and when I do it without it takes 0.005 seconds, although both real fast, doing the php way is marginably faster, that's why I said slightly.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now