Solved

Convert MYSQL DATETIME to short date time

Posted on 2009-04-10
3
1,606 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 500 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 110

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

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.

Question has a verified solution.

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

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 i…
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 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 …

695 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