Link to home
Create AccountLog in
PHP

PHP

--

Questions

--

Followers

Top Experts

Avatar of BGHTechGuru
BGHTechGuruπŸ‡ΊπŸ‡Έ

mySQL :: Order By Date / Time ::
Okay, working on a project that has a different timestamp than the standard mysql timestamp.

Format: 03/11/2009 at 10:24:12 AM

I need to have it so the latest Date/Time shows on Top.

SQL: SELECT `Date`, FROM `TABLENAME` ORDER BY `Date` DESC

SQL Current Result:
03/11/2009 at 10:24:12 AM
03/11/2009 at 10:21:25 AM
03/11/2009 at 10:14:08 AM
03/11/2009 at 01:13:34 PM

SQL Expected Result:
03/11/2009 at 01:13:34 PM
03/11/2009 at 10:24:12 AM
03/11/2009 at 10:21:25 AM
03/11/2009 at 10:14:08 AM

If you need any additional information, please let me know.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of elbrian87elbrian87πŸ‡ΊπŸ‡Έ

Change Β "DESC" to "ASC", should fix it :)

Avatar of elbrian87elbrian87πŸ‡ΊπŸ‡Έ

Oh I see now- I don't think my suggestion will work. Sorry about that- I misunderstood.

Avatar of BGHTechGuruBGHTechGuruπŸ‡ΊπŸ‡Έ

ASKER

That produces a sort of correct result:

03/11/2009 at 01:13:34 PM
03/11/2009 at 10:14:08 AM
03/11/2009 at 10:21:25 AM
03/11/2009 at 10:24:12 AM

When I need it to be:
03/11/2009 at 01:13:34 PM
03/11/2009 at 10:24:12 AM
03/11/2009 at 10:21:25 AM
03/11/2009 at 10:14:08 AM

Notice the AM/PM Change. That might of threw you off :)

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of elbrian87elbrian87πŸ‡ΊπŸ‡Έ

It most certainly did. If you dont mind my asking, what type of field is this information stored in, in the table?

Is it a VARCHAR? Also, how is the time being inserted into it? Is it just like what's being returned, or is the info you're showing us post-parsed into a readable format?

Avatar of BGHTechGuruBGHTechGuruπŸ‡ΊπŸ‡Έ

ASKER

Its a VARCHAR(25). It's inserted as it's shown. That's a straight copy and paste from the table, just the table name is changed. :)

Avatar of elbrian87elbrian87πŸ‡ΊπŸ‡Έ

OK- I found the two functions you will need to research to make this work. I began writing a script but this really is pretty involved and would take a bit of time.

These functions are extremely useful, anyways- so you should learn them to buff up on parsing data with PHP :)

Link 1- use this to separate the time format you have into sections (month, day, hour, minute, am/pm, etc)
http://us2.php.net/manual/en/function.explode.php

Link 2- use this to change * AM to "0" and * PM to "+12" (hours) to convert the time to a univeral (24 hour, IE 13:45 instead of 1:45PM) time standard
http://us2.php.net/strrpos

Then make a basic variable to sort them.

I wish I could help more but I don't have that much time to commit, on top of my own project :/

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of tkalchevtkalchevπŸ‡©πŸ‡ͺ

try

select str_to_date( `Date`, "%m/%d/%Y at %h:%i:%s %p" ) as date1 from tablename order by date1 desc

Avatar of BGHTechGuruBGHTechGuruπŸ‡ΊπŸ‡Έ

ASKER

tkalchev,

I tried your solution, but no prevail. It returns rows, but the values are NULL.

Avatar of tkalchevtkalchevπŸ‡©πŸ‡ͺ

which mysql version do you have? I've tested it on 5.0 and it woks perfect

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of BGHTechGuruBGHTechGuruπŸ‡ΊπŸ‡Έ

ASKER

Hmm, didn't think of a version issue.

MySQL client version: 4.1.22

ASKER CERTIFIED SOLUTION
Avatar of tkalchevtkalchevπŸ‡©πŸ‡ͺ

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of BGHTechGuruBGHTechGuruπŸ‡ΊπŸ‡Έ

ASKER

Nice tkalchev...

It works perfectly, then I just pass in the returned row value to the following function I created.

Final Solution:



// RUN QUERY
SELECT STR_TO_DATE(replace(`Date`," at ",""), "%m/%d/%Y %h:%i:%s %p" ) as DateAdd FROM `TABLENAME` ORDER BY `DateAdd` DESC
 
// WHILE LOOP
$strDateTime = FormatDateTime($Row["DateAdd"]);
 
function FormatDateTime($string) {
		$strDate = substr($string,0,10);
		$strTime = trim(substr($string,10));
		
		list($strYear, $strMonth, $strDay) = explode('-', $strDate);
 
		$strTime = date('g:i:s A' , strtotime($strTime));
		
		$string = $strMonth . "/" . $strDay . "/" . $strYear . " at " . $strTime;
		return $string;
	}

Open in new window


Avatar of BGHTechGuruBGHTechGuruπŸ‡ΊπŸ‡Έ

ASKER

After running it again, I realize I'm kind of in the same situation still...

SELECT str_to_date( replace( `rs_DateAdd` , ' at ', '' ) , '%m/%d/%Y %h:%i:%s %p' ) AS Date
FROM `TABLENAME`
WHERE `rs_ExtID` =79
ORDER BY `rs_DateAdd` DESC

Results:
Date
2009-03-12 09:52:22
2009-03-11 10:24:12
2009-03-11 10:21:25
2009-03-11 10:14:08
2009-03-11 13:13:34
2009-03-10 13:24:23
2009-03-10 13:23:08
2009-03-05 09:45:21

Am I thinking about this wrong? I need the date Descending, but the time from Latest to earliest.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of BGHTechGuruBGHTechGuruπŸ‡ΊπŸ‡Έ

ASKER

Okay, my bad.. You're still correct. I messed up the SQL when putting it in the second time.

SELECT str_to_date( replace( `rs_DateAdd` , ' at ', '' ) , '%m/%d/%Y %h:%i:%s %p' ) AS Date
FROM `TABLENAME`
WHERE `rs_ExtID` =79
ORDER BY `Date` DESC

Thanks for the help!

Avatar of tkalchevtkalchevπŸ‡©πŸ‡ͺ

SELECT str_to_date( replace( `rs_DateAdd` , ' at ', '' ) , '%m/%d/%Y %h:%i:%s %p' ) AS Date
FROM `TABLENAME`
WHERE `rs_ExtID` =79
ORDER BY `Date` DESC
PHP

PHP

--

Questions

--

Followers

Top Experts

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.