Results 1 to 14 of 14

Thread: problem displaying records in order of time and date in php

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    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.

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: problem displaying records in order of time and date

    Quote Originally Posted by john tindell
    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
    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

  4. #4
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: problem displaying records in order of time and date

    Quote 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.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: problem displaying records in order of time and date

    Quote 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:
    1. 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.

    2. 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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    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!!

  8. #8
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: problem displaying records in order of time and date in php

    Quote 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.

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: problem displaying records in order of time and date in php

    I second that

  12. #12
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Re: problem displaying records in order of time and date in php

    Quote 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?
    ?
    'What's this bit for anyway?
    For Jono

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    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.

  14. #14
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    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
  •  



Click Here to Expand Forum to Full Width