Solved

how to format dates using php + mysql

Posted on 2007-11-27
8
1,419 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]
Comment Utility
0
 

Author Comment

by:markmchugh
Comment Utility
can i do it in php?
0
 
LVL 20

Expert Comment

by:Muhammad Wasif
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 20

Expert Comment

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

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
>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
Comment Utility
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
Comment Utility
@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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
echo button 13 45
How to validate 2 forms at once 8 25
windows 10 bash shell 4 32
update field on focusout 15 19
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 look for a specific file type in a local or remote server directory using PHP.

772 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

15 Experts available now in Live!

Get 1:1 Help Now