Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Help with Putting Dates in Order (todays 1st for e.g)

    Hi,
    I hope someone can help me out, this script gathers infomation from a flat text file and displays it within a table, but i would like to have the dates field displayed in Date Time order, but i just cant get it to work.

    Dates are presented like this: October 15 2009, 08:21

    Please Help.
    Jenna X

    Script below:

    <?php
    $sortby = $_SERVER['QUERY_STRING'];

    $set = '';
    if ($sortby == '') {
    $header = 'Date';
    $sortby = 'date';
    $set = 'y';
    }
    if ($sortby == 'network') {
    $header = 'Network';
    $set = 'y';
    }
    if ($sortby == 'userID') {
    $header = 'UserID';
    $set = 'y';
    }
    if ($sortby == 'sale') {
    $header = 'Sale';
    $set = 'y';
    }
    if ($sortby == 'saleID') {
    $header = 'SaleID';
    $set = 'y';
    }
    if ($sortby == 'content') {
    $header = 'Content';
    $set = 'y';
    }
    if ($sortby == 'site') {
    $header = 'Site';
    $set = 'y';
    }
    if ($set == '') {
    echo 'The variable at the end of the web address did not match one required by the code. Please check the web address for errors.';
    exit;
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Untitled</title>


    <style type="text/css">
    <!--

    body, th, td, p, small {
    font-family:'Arial';
    font-size:10;
    color:#000000;
    text-align: center;
    }
    small {font-size:10;}
    table {
    border:1px solid #ccf;
    padding:3px;
    }
    td, th {
    background-color:#FFFFFF;
    padding:7px 20px 7px 20px;
    }
    th {background-color:#E9D2FF; color:#000000;}

    h1 {font-size:10; color:#000000;}
    h1 .sortby {color:#000000;}
    .style1 {color: #000000}
    body,td,th {
    color: #000000;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    }
    body {
    background-color: #FFFFFF;
    }
    a:link {
    color: #360051;
    }
    a:visited {
    color: #360051;
    }
    a:hover {
    color: #360051;
    }
    a:active {
    color: #360051;
    }
    .style2 {color: #000000; font-size: 16px; }
    -->
    </style>

    </head>

    <body>



    <div align="center">
    <?php
    echo '



    <table summary="List of demo fields">
    <tr>
    <th><a href="?">Date[/url]</th>
    <th><a href="?network">Network[/url]</th>
    <th><a href="?userID">UserID[/url]</th>
    <th><a href="?sale">Sale[/url]</th>
    <th><a href="?saleID">SaleID[/url]</th>
    <th><a href="?content">Content[/url]</th>
    <th><a href="?site">Site[/url]</th>
    </tr>';

    $fp = fopen('logs.txt','r');
    if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}

    while (!feof($fp)) {
    $line = fgets($fp,1024); //use 2048 if very long lines
    $row++;
    list ($date, $network, $userID, $sale, $saleID, $content, $site) = split ('\|', $line);
    if ($sortby == 'date') $sortkey = strtotime($Date);
    if ($sortby == 'network') $sortkey = strtolower($Network);
    if ($sortby == 'userID') $sortkey = strtolower($UserID);
    if ($sortby == 'sale') $sortkey = $Sale;
    if ($sortby == 'saleID') $sortkey = strtolower($SaleID);
    if ($sortby == 'content') $sortkey = strtolower($Content);
    if ($sortby == 'site') $sortkey = $Site;
    $col[$row] = array($sortkey, $date, $network, $userID, $sale, $saleID, $content, $site);
    }

    fclose($fp);

    sort($col);
    reset ($col);

    $arrays = count($col) - 1;

    $loop = -1;
    while ($loop < $arrays) {
    $loop++;
    echo '
    <tr>
    <td>'.$col[$loop][1].'</td>
    <td>'.$col[$loop][2].'</td>
    <td>'.$col[$loop][3].'</td>
    <td>'.$col[$loop][4].'</td>
    <td>'.$col[$loop][5].'</td>
    <td>'.$col[$loop][6].'</td>
    <td>'.$col[$loop][7].'</td>
    </tr>';
    }

    echo '
    </table>

    ' ?>
    </div>

    </body>
    </html>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,465
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Is the file you're using called "logs.txt"?

    If so, display a few lines from that log, exactly as they appear in that file.
    I can then see what has to happen with it.

  • #3
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Ok, thankyou,

    Here is a few lines of data form logs.txt:

    October 15 2009, 08:31 | GBT_BHBROAD | 35317974 | 198.260869565217 |692745479 | Dual Action Fruit Spray | g1home
    October 14 2009, 08:21 | GBT_BHBROAD | 35317974 | 198.260869565217 | 692745479 | Dual Action Fruit Spray | g1home
    October 14 2009, 08:23 | GBT_BHBROAD | 35317976 | 198.260869565217 | 692745479 | Dual Action Fruit Spray | g1home
    May 24 2009, 08:04 | GBT_VOD | 356286415 | 198.260869565217 | 692745479 | Dual Action Fruit Spray | g1home
    January 19 2009, 08:08 | GBT_VOD | 356286415 | 198.260869565217 | 692745479 | Dual Action Fruit Spray | g1home

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,465
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    OK ... give me an hour or so and I'll have it "sorted out".
    Would it be sorted ascending or descending by date? (oldest date first?)

  • #5
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Newest date first please.

    Thanks so much mlseim!

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,465
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    The key to sorting is to change your long text date format into a single
    UNIX timestamp integer. This makes sorting a snap.

    First, put this function at the bottom of your script.
    PHP Code:
    function DateToTimeStamp($date){
    // Convert a date in this format: "October 15 2009, 12:30"
    // To a UNIX TimeString (eg. 1255624200)

    $monthnames = array('blank','January','February','March','April','May','June','July','August','September','October','November','December');

    // Separate the date from the time.
    $dt=explode(",",$date);

    // Separate the month, day, year.
    $date=explode(" ",$dt[0]);
    // Convert month text to a month number (1=January).
    $monthnumber array_search($date[0],$monthnames);

    // Separate the Hour from Minute
    $time=explode(":",$dt[1]);

    // Generate a UNIX TimeStamp
    $new_time=mktime($time[0],$time[1],0,$monthnumber,$date[1],$date[2]);
    return(
    $new_time);


    Then, change this section to what I have here ...
    I also noticed you have Capitalized variables in your "if" statements, but not in the "list" line. They need to be the same (so I altered those too) ...
    PHP Code:
    while (!feof($fp)) {
    $line fgets($fp,1024); //use 2048 if very long lines
    $row++;
    list (
    $date$network$userID$sale$saleID$content$site) = split ('\|'$line);
    if (
    $sortby == 'date'$sortkey DateToTimeStamp($date);
    if (
    $sortby == 'network'$sortkey strtolower($network);
    if (
    $sortby == 'userID'$sortkey strtolower($userID);
    if (
    $sortby == 'sale'$sortkey $sale;
    if (
    $sortby == 'saleID'$sortkey strtolower($SaleID);
    if (
    $sortby == 'content'$sortkey strtolower($content);
    if (
    $sortby == 'site'$sortkey $site;
    $col[$row] = array($sortkey$date$network$userID$sale$saleID$content$site);


  • #7
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Thankyou so much, it has put them in date order

    but i do get a warning error!

    Warning: mktime() expects parameter 1 to be long, string given in /home/smartmob/public_html/manage/stats/g1sexhome.php on line 180

    Have i put the everything in the coorect place! Code below

    Many thanks
    Jenna X

    <?php
    $sortby = $_SERVER['QUERY_STRING'];

    $set = '';
    if ($sortby == '') {
    $header = 'Date';
    $sortby = 'date';
    $set = 'y';
    }
    if ($sortby == 'network') {
    $header = 'Network';
    $set = 'y';
    }
    if ($sortby == 'userID') {
    $header = 'UserID';
    $set = 'y';
    }
    if ($sortby == 'sale') {
    $header = 'Sale';
    $set = 'y';
    }
    if ($sortby == 'saleID') {
    $header = 'SaleID';
    $set = 'y';
    }
    if ($sortby == 'content') {
    $header = 'Content';
    $set = 'y';
    }
    if ($sortby == 'site') {
    $header = 'Site';
    $set = 'y';
    }
    if ($set == '') {
    echo 'The variable at the end of the web address did not match one required by the code. Please check the web address for errors.';
    exit;
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Untilted</title>


    <style type="text/css">
    <!--

    body, th, td, p, small {
    font-family:'Arial';
    font-size:10;
    color:#000000;
    text-align: center;
    }
    small {font-size:10;}
    table {
    border:1px solid #ccf;
    padding:3px;
    }
    td, th {
    background-color:#FFFFFF;
    padding:7px 20px 7px 20px;
    }
    th {background-color:#E9D2FF; color:#000000;}

    h1 {font-size:10; color:#000000;}
    h1 .sortby {color:#000000;}
    .style1 {color: #000000}
    body,td,th {
    color: #000000;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11px;
    }
    body {
    background-color: #FFFFFF;
    }
    a:link {
    color: #360051;
    }
    a:visited {
    color: #360051;
    }
    a:hover {
    color: #360051;
    }
    a:active {
    color: #360051;
    }
    .style2 {color: #000000; font-size: 16px; }
    -->
    </style>

    </head>

    <body>


    <div align="center">
    <?php
    echo '



    <table summary="List of demo fields">
    <tr>
    <th><a href="?">Date</a></th>
    <th><a href="?network">Network</a></th>
    <th><a href="?userID">UserID</a></th>
    <th><a href="?sale">Sale</a></th>
    <th><a href="?saleID">SaleID</a></th>
    <th><a href="?content">Content</a></th>
    <th><a href="?site">Site</a></th>
    </tr>';

    $fp = fopen('logs.txt','r');
    if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}

    while (!feof($fp)) {
    $line = fgets($fp,1024); //use 2048 if very long lines
    $row++;
    list ($date, $network, $userID, $sale, $saleID, $content, $site) = split ('\|', $line);
    if ($sortby == 'date') $sortkey = DateToTimeStamp($date);
    if ($sortby == 'network') $sortkey = strtolower($network);
    if ($sortby == 'userID') $sortkey = strtolower($userID);
    if ($sortby == 'sale') $sortkey = $sale;
    if ($sortby == 'saleID') $sortkey = strtolower($SaleID);
    if ($sortby == 'content') $sortkey = strtolower($content);
    if ($sortby == 'site') $sortkey = $site;
    $col[$row] = array($sortkey, $date, $network, $userID, $sale, $saleID, $content, $site);
    }


    fclose($fp);

    sort($col);
    reset ($col);

    $arrays = count($col) - 1;

    $loop = -1;
    while ($loop < $arrays) {
    $loop++;
    echo '
    <tr>
    <td>'.$col[$loop][1].'</td>
    <td>'.$col[$loop][2].'</td>
    <td>'.$col[$loop][3].'</td>
    <td>'.$col[$loop][4].'</td>
    <td>'.$col[$loop][5].'</td>
    <td>'.$col[$loop][6].'</td>
    <td>'.$col[$loop][7].'</td>
    </tr>';
    }
    function DateToTimeStamp($date){
    // Convert a date in this format: "October 15 2009, 12:30"
    // To a UNIX TimeString (eg. 1255624200)

    $monthnames = array('blank','January','February','March','April','May','June','July','August','September','October ','November','December');

    // Separate the date from the time.
    $dt=explode(",",$date);

    // Separate the month, day, year.
    $date=explode(" ",$dt[0]);
    // Convert month text to a month number (1=January).
    $monthnumber = array_search($date[0],$monthnames);

    // Separate the Hour from Minute
    $time=explode(":",$dt[1]);

    // Generate a UNIX TimeStamp
    $new_time=mktime($time[0],$time[1],0,$monthnumber,$date[1],$date[2]);
    return($new_time);
    }

    echo '</table>'


    ?>
    </div>

    </body>
    </html>

  • #8
    New Coder
    Join Date
    Oct 2009
    Location
    UK
    Posts
    90
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Sorry 1 more thing, can you make so the todays date show at the top of the list.

    October 15 2009, 8:03
    March 13 2009, 8,04
    January 12 2009, 8:08

    Thankyou,

    I did just post another comment before this, with an error warning i'm getting and the code, but i think a moderator has to aprove it 1st.

    Warning: mktime() expects parameter 1 to be long, string given in /home/smartmob/public_html/manage/stats/g1sexhome.php on line 181


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •