Solved

how to format dates using php + mysql

Posted on 2007-11-27
8
1,420 Views
Last Modified: 2010-10-25
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
Comment
Question by:markmchugh
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20360347
0
 

Author Comment

by:markmchugh
ID: 20360369
can i do it in php?
0
 
LVL 20

Expert Comment

by:Muhammad Wasif
ID: 20360384
Why you want to do it in PHP? Anyway take a look at this http://www.php.net/manual/en/ref.datetime.php
0
 
LVL 17

Expert Comment

by:nplib
ID: 20360394
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 20

Expert Comment

by:Muhammad Wasif
ID: 20360414
@nplib
Is it faster to format the date in PHP rather than MySQL? How?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20360416
>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
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
ID: 20360495
strtotime() as angellll said:
<?php

	$date = "2007-11-27 00:00:00";

	

	echo date("d-m-Y",strtotime($date));

?>

Open in new window

0
 
LVL 17

Expert Comment

by:nplib
ID: 20360578
@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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now