Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to format dates using php + mysql

Posted on 2007-11-27
8
Medium Priority
?
1,427 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 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

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

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!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

972 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