?
Solved

Convert MYSQL DATETIME to short date time

Posted on 2009-04-10
3
Medium Priority
?
1,626 Views
Last Modified: 2012-06-27
I've extracted a table from mySQL.  One of the fields is of type DATETIME.

I'm trying to convert this to a short date and time with code below.

However, the date returned with this code is 01/01/70 1:33 (AM) for all dates.

What am I doing wrong?   Would it better to format the data in the SQL query - if so any ideas how to do this?
while ($table = $result->fetch_row()) {
//
//
//
field4 = date("d/m/y g:i (A)", $table[3]);

Open in new window

0
Comment
Question by:lee_jd
[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 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24119702
You probably need strtotime():

$field4 = date("d/m/y g:i (A)", strtotime($table[3]));

To do it in mysql:

select date_format(DateCol,'%d/%m/%y %h:%i (%p)') as DateCol
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24120172
You can do the date formatting either way.  The choice might depend on whether you need to collate or compare the date values elsewhere in the PHP code.  If you needed the ISO8601-format date, you might select the DATETIME field directly and make a "pretty date" conversion in PHP.  That would probably be my instinctive choice.  But either way is likely to get you the right values.  Cheers, ~Ray
0
 
LVL 2

Author Closing Comment

by:lee_jd
ID: 31569079
Top class
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

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…
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…
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 dynamically set the form action using jQuery.
Suggested Courses

777 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