Solved

Convert MYSQL DATETIME to short date time

Posted on 2009-04-10
3
1,540 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
Comment Utility
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
Comment Utility
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
Comment Utility
Top class
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 look for a specific file type in a local or remote server directory using PHP.

763 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

12 Experts available now in Live!

Get 1:1 Help Now