SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    converting Timestamp into useful information

    I have a mySQL database with a table which has a timestamp function-row. Everytime a user logs in, the timestamp is automatically set.

    Now, I want to use this timestamp for an admin-function to see when users have last logged in. The current timestamp has the following (standard) order:
    20010718110445
    YYYYMMDDHHMMSS (year, month, day, hour, minute, second)

    How do I break this up into useable parts? Say I want to show the date (15-01-2001) and time (18:00) the user has last logged in.

    Any help appreciated!

    Jazz

  2. #2
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey,

    i don't have my php reference book with me right now, so this post may be a little crude. i believe that there is a substring function called: substr(string, starting point). if i remember correctly, it takes 2 parameters, a string and the location where you want it to start from...but i am now realizing this won't work for you.

    i know that there is a similar function along these lines, and I think that you can give it a string, a starting point and the # of characters you want it to read. what i am thinking is that since you know what the format of this sting is going to be all of the time, you can use this function (which i can't think of) to set variables equal to different parts of the datestamp. For example:
    $year = fuctionIcantThinkOf(dateStamp, startPoint, #ofCharacters);

    I will take a look at my book tonight and post the function if I find it. In the meantime, check out the substr() function and functions like it.

  3. #3
    SitePoint Member
    Join Date
    Jul 2001
    Location
    Bournemouth UK
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know how to use the time stamp function but i held the time someone last logged in as an integer which is the unix time stamp (number of seconds from midnith 01 01 1970) in my mysql database then used the strftime function to output it into some thing readable

    $loginat = time();

    $date_time_array = getdate($loginat);

    echo "Time now :".strftime( "%H:%M %A %d %B",$loginat);

    hop this helps!

    Tris

  4. #4
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This function can help you:
    PHP Code:
    <?
    function thetime($unixts){

    // unixtimestamp to readable timestamp
    // coded by iBram007 [http://bramstart.be/ibwds]

        
    $dateformat "d/m/Y";
        
    $timeformat "H:i";

        
    $thedate date($dateformat$unixts);
        
    $thetime date($timeformat$unixts);

        
    $print  $thedate;
        
    $print .= " - ";
        
    $print .= $thetime;
        echo 
    $print;

    }
    ?>
    You can adapt $dateformat and $timeformat if you want.

    more info of the format: http://www.php.net/manual/en/function.date.php

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also a much easier way to do this is either to convert the timestamp to a unix timestamp that can be used with PHP date() function.

    SELECT, field1, field2, UNIX_TIMESTAMP(datefield) as date from tablename

    Now when you get the data back you will have a field in the resultset named date with a unix timestamp made from the timestamp you have in your table. So you can use this timestamp with the date() function.

    date("Y-m-d", $unixtimestampfromdatabase);


    Or you can format your date right in your query using DATE_FORMAT()

    SELECT, field1, field2, DATE_FORMAT(datefield, '%Y-%m-%d') as date from tablename

    This would return the format you are looking for, but since you want the individual elements, the UNIX_TIMESTAMP() function is the most efficient method to achieve this.

    Here is some more info on the MYSQL date functions.

    http://mysql.he.net/documentation/my...time_functions
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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