Solved

Format - and - Unformat a MySQL datetime

Posted on 2004-09-01
6
504 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.

762 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

17 Experts available now in Live!

Get 1:1 Help Now