Solved

Convert MYSQL DATETIME to short date time

Posted on 2009-04-10
3
1,553 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
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Blog posts not showing up on Blog page 7 51
mysqli 3 18
Split long text string into shorter chunks 7 21
javascript date picker in php while loop 5 22
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
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.

914 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

23 Experts available now in Live!

Get 1:1 Help Now