|
-
Jul 24th, 2006, 03:50 AM
#1
Thread Starter
Frenzied Member
problem displaying records in order of time and date in php
Hi all.I use $Timestamp=date("g:i A l, F j Y."); to collect time and date the visitors entered my site . It inserts the data in to mysql database in this format :
3:02 AM Monday, July 24 2006
Code:
mysql_query("INSERT INTO logdisplay VALUES('$ID','$column1','$Timestamp','$column2','$columns3')");
$affected_rows = $sql->a_rows;
But when i query the database and use order by ,the data does not get displaied in order of newest on the top!!
Code:
select * from logdisplay order by Timestamp
could any one help me so that i get newst log records on the top .In another how to query my table baced on time an data.Thanks
Last edited by tony007; Jul 25th, 2006 at 03:15 AM.
-
Jul 24th, 2006, 03:57 AM
#2
<?="Moderator"?>
Re: problem displaying records in order of time and date
You should try storing timestamp in the database andf format it when you get it out, this will also allow you to more efficiantly sort your table by date.
MySQL has built in fucntions for formatting date,
http://dev.mysql.com/doc/refman/5.0/...functions.html
-
Jul 24th, 2006, 04:04 AM
#3
Thread Starter
Frenzied Member
Re: problem displaying records in order of time and date
 Originally Posted by john tindell
Thank u for u reply. This is exact way of i store time and date in my database table :3:02 AM Monday, July 24 2006
do u mean that is incorect ? could u show me how display my data baced on time and data and be able to output records of one months only too.Thanks
-
Jul 24th, 2006, 04:35 AM
#4
Re: problem displaying records in order of time and date
You sould store the time a user enters your site using Time() then once you query your database use the Date() function to format the time to what you want.
-
Jul 24th, 2006, 06:24 PM
#5
Thread Starter
Frenzied Member
Re: problem displaying records in order of time and date
 Originally Posted by lintz
You sould store the time a user enters your site using Time() then once you query your database use the Date() function to format the time to what you want.
could u show me how i can use time . If i do not record date then how i can query the database baced on date? could u explain this to me more.thanks
Last edited by tony007; Jul 25th, 2006 at 07:02 AM.
-
Jul 25th, 2006, 05:22 AM
#6
Re: problem displaying records in order of time and date
 Originally Posted by tony007
Thank u for u reply. This is exact way of i store time and date in my database table :3:02 AM Monday, July 24 2006
do u mean that is incorect ? could u show me how display my data baced on time and data and be able to output records of one months only too.Thanks
You have obviously set the column / field type to text. This means that any sort on the data will be alphabetical. You can do one of two things:
- Change the timestamp column to a TIMESTAMP data type or a DATETIME as detailed here: http://dev.mysql.com/doc/refman/5.0/...ime-types.html. You can then use MySql's date & time functions within your queries to format the dates as you need them and/or sort them.
- The second option is to use a UNIX timestamp. As you are dealing with dates after 13 Dec 1901, you won't have any problems. You can obtain a unix timestmap in PHP using the time() function, like lintz has already mentioned. You then need to store this in your database as an integer (this means you will need to change the timestamp field to an integer). Again, when formatting the timestmap you can use MYSql's date & time functions within the queries or you can use the date() function from within PHP.
-
Jul 25th, 2006, 07:05 AM
#7
Thread Starter
Frenzied Member
Re: problem displaying records in order of time and date in php
Thanks visual . Is there a way to save current time in one column and date in seperate column and then when querying the logdisplay i just sort baced on date or time ? That would be easier way but i do not how to save those seperatly and later sort them baced on date and time!!
-
Jul 26th, 2006, 05:59 AM
#8
Re: problem displaying records in order of time and date in php
No need to save te date and time in seperate fields because if you save using UNIX timestamp then when you format the data you can format to display only the date, only the time or a combination.
-
Jul 26th, 2006, 08:14 AM
#9
Thread Starter
Frenzied Member
Re: problem displaying records in order of time and date in php
 Originally Posted by lintz
No need to save te date and time in seperate fields because if you save using UNIX timestamp then when you format the data you can format to display only the date, only the time or a combination.
man i am not running unix server !! not i know if the hosting company is on unix!! fuethermore, how to record time and date so i can output my records in order of time and date. could any one just show me the insert statement and datatype and how to query the table so i get out put in order of time and date.
-
Jul 26th, 2006, 08:36 AM
#10
Re: problem displaying records in order of time and date in php
We have already told you how to do it - you have been given several solutions infact. You do not need UNIX to use a unix timestamp; the unix timestamp is just a format used to store a date and time (it happens to be an integer corresponding to the number of seconds which have elapsed since 1 Jan 1970).
So if you use a UNIX timestamp, all you need to to is store it in your database as an integer and sort it as you would any other data type using the ORDER BY clause.
Give it a go and if you hit any problems post back; I for one will not just give you code and SQL to copy and paste into your scripts
-
Jul 27th, 2006, 02:34 AM
#11
Re: problem displaying records in order of time and date in php
I second that
-
Jul 29th, 2006, 12:05 PM
#12
Fanatic Member
Re: problem displaying records in order of time and date in php
 Originally Posted by tony007
Code:
select * from logdisplay order by Timestamp
Surely the answer to get newest first would be
Code:
SELECT * FROM logdisplay ORDER BY Timestamp DESC
as the newer dates are a bigger number...
or did I miss something when I thought this threasd went off on a tangent?
-
Jul 29th, 2006, 12:44 PM
#13
Thread Starter
Frenzied Member
Re: problem displaying records in order of time and date in php
I used unix time like and i got the date in this format :1154194814
mysql_query("INSERT INTO visitor VALUES('$ID', '$HTTP_X_FORWARDED_FOR','". time() ."','$countryName', '$country')");
do u guys think that is corect . so when i query in order of newese on the top should i do like this? :
SELECT * FROM visitor ORDER BY time DESC
do u guys think that will sort my data acording to both time and date ?
Last edited by tony007; Jul 29th, 2006 at 01:01 PM.
-
Jul 29th, 2006, 07:08 PM
#14
Re: problem displaying records in order of time and date in php
Yes!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|