Solved

how to format dates using php + mysql

Posted on 2007-11-27
8
1,424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 143

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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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
 
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 143

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

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
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 count occurrences of each item in an array.

632 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