Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Format - and - Unformat a MySQL datetime

Posted on 2004-09-01
6
Medium Priority
?
544 Views
Last Modified: 2013-12-12
Hi, I am struggling with converting mysql datetime formats into dd-mm-yyyy hh:mm:ss format
I have a small web app (PHP) that displays a list of arrival and departure times for aircraft
The format in the database is YYYY-MM-DD hh:mm:ss but I want to display it as dd-mm-yyyy hh:mm:ss
When I try to convert it  (using a timestamp of 1975-01-01 10:00:01) I get a display of 1-1-1970 10:32:55
I also need to convert this format back so I can enter it into the database
Please help!!!!
0
Comment
Question by:maunded
[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
  • 3
  • 3
6 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11959743
Hi

You can try something like this:

mysql_query("SELECT DATE_FORMAT(the_date,'%D,%M,%Y %T') FROM table_name WHERE this='that'");

More information on date_format
http://dev.mysql.com/doc/mysql/en/Date_and_time_functions.html

Regards,
Zyloch
0
 
LVL 1

Author Comment

by:maunded
ID: 11959865
Heres what I have come up with so far:

function ConvFromMySQL($sqldate) {
      $format= "d-m-Y h:i:s";
$mydate = date($format,strtotime($sqldate));
return $mydate;
};
This is fine, it convers the datetime field to d-m-Y h:i:s no problem.
Converting back the other way to allow for the insert to work properly is a problem...can someone tell me why this dosent work?

function ConvToMySQL($phpdate) {
      $format= "Y-m-d h:i:s";
$mydate = date($format,strtotime($phpdate));
return $mydate;
};

Thanks
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11959966
I can't be sure. Try doing this:

$format="YmdHis";
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!

 
LVL 1

Author Comment

by:maunded
ID: 11960009
I got it:
function ConvFromMySQL($sqldate) {
      $format= "d-m-Y H:i:s";
$mydate = date($format,strtotime($sqldate));
return $mydate;
};

function ConvToMySQL($phpdate) {

$array = preg_split("/[\s\/\.\-:]/", $phpdate);
$mydate = "$array[2]-$array[1]-$array[0] $array[3]:$array[4]:$array[5]";
return $mydate;
};

Its messy I think, but it works!
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 2000 total points
ID: 11960024
Nice job, heh, It actually isn't very messy at all
0
 
LVL 1

Author Comment

by:maunded
ID: 11960031
Accepted yours since you were the first (and only) to answer :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

660 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