Solved

Format - and - Unformat a MySQL datetime

Posted on 2004-09-01
6
516 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

830 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