Solved

Format - and - Unformat a MySQL datetime

Posted on 2004-09-01
6
512 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

805 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