SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Thread: current date

  1. #1
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question current date

    PHP Code:
            $rs1mysql_query("UPDATE profile set dateoflastlogin='".$t."',last_loginid ='".$userid."' where username='$username'"); 
    how can i insert the current date in the database
    the column in database is "dateoflastlogin timestamp(8) not null"
    wat shud $t be?
    time() command gives the time in secs and hence cannot be updated to my database

  2. #2

  3. #3
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $rs1mysql_query("UPDATE profile set dateoflastlogin=NOW(),last_loginid ='".$userid."' where username='$username'"); 

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    As you require a timestamp...
    PHP Code:
    <?php
    $sSQL 
    sprintf(
        
    "UPDATE profile SET dateoflastlogin = FROM_UNIXTIME(%s), last_loginid = %s WHERE username = '%s' LIMIT 1",
        
    time(),
        (Integer)
    $userid,
        
    mysql_real_escape_string($username)
    );
    $rResult mysql_query($sSQL);
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    thanks it worked
    now i want to get the data from the database and compare it with present time
    and get their difference in mins
    how do i do this?
    m using tis coding to convert the diff into years but for mins wat to do?
    PHP Code:
    $timestamp strtotime($row2["dateoflastlogin"]);
        
        
    $diff time() - $timestamp;
        
        
    $ysec 60 60 24 *365
        
    $years floor($diff $ysec); 

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Something along these lines should do it...
    PHP Code:
    <?php
    $rResult 
    mysql_query("SELECT UNIX_TIMESTAMP(dateOfLastLogin) AS lastLoginTime FROM table WHERE user = 'SilverBulletUK' LIMIT 1");
    while (
    $aRecord mysql_fetch_assoc($rResult))
    {
        
    printf(
            
    'You last logged in %s minute(s) ago.',
            
    floor((time() - $aRecord['lastLoginTime']) / 60)
        );
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this


    PHP Code:
    $timestamp =mysql_query("SELECT TIMESTAMPDIFF(MINUTE, dateoflastlogin, NOW()) AS MinsSinceLastLogin FROM profile ORDER BY MinsSinceLastLogin DESC LIMIT 0,5"

  8. #8
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    i din not understand at all
    i need to store the difference and check whether it is equal to 1440 mins(24hrs) or not
    PHP Code:
     $username=$_SESSION["username"];
      
    $rs2=mysql_query("select * from profile where username='$username'");
        
    $row2 =mysql_fetch_array($rs2);    
        
    $timestamp strtotime($row2["dateoflastlogin"]);
        
    $diff time() - $timestamp;
        
    $ysec 60 60*24 
        
    $mins floor($diff $ysec);
        
    $t1=time();
        if ((
    $mins 1440 && $row2["last_loginid"]!=$userid))
        {
            
    header("Location: ../profile/error.php");
        }
        elseif(
    $mins>=1440){
                
    $t=date('Y-m-d-h-m-s'$t1);        
            
    $rs1mysql_query("UPDATE profile set dateoflastlogin='".$t."',last_loginid ='".$userid."' where username='$username'");

    Last edited by sneha24; May 18, 2009 at 02:58.

  9. #9
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    plz reply

  10. #10
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i need to store the difference and check whether it is equal to 1440 mins(24hrs) or not
    you really want to store this different into database or just calculate in to show users logged in time?

    like this logged in since 2 hours 34 min

    is this you want to do or something else?

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    What about?

    PHP Code:
    <?php
    $rResult 
    mysql_query(
        
    sprintf(
            
    "SELECT TIMESTAMPDIFF(DAY, dateoflastlogin, FROM_UNIXTIME(%s))UNIX_TIMESTAMP(dateOfLastLogin) AS daysSinceLastLogin FROM profile WHERE user = '%s' LIMIT 1",
            
    time(),
            
    mysql_real_escape_string($username)
        )
    );
    $aRecord mysql_fetch_assoc($rResult);
    if(
    $aRecord['daysSinceLastLogin'] >= 1)
    {
        
    $sSQL sprintf(
            
    "UPDATE profile SET dateoflastlogin = FROM_UNIXTIME(%s), last_loginid = %s WHERE username = '%s' LIMIT 1",
            
    time(),
            (Integer)
    $userid,
            
    mysql_real_escape_string($username)
        );
        
    $rResult mysql_query($sSQL);
    }
    ?>
    If last log in was over 1 or more day(s) ago, update last log in time to now?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #12
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @deep
    i don need to store into database
    all i need to do is calculate and show the results to user

  13. #13
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @silverbullet
    what is &#37;s in "FROM_UNIXTIME(%s))UNIX_TIMESTAMP(dateOfLastLogin)"

  14. #14
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try altering the code I posted to meet your requirements, if it doesn't work, post what you have here and see what others suggest.

    You really need to understand how the code works if you're going to use it...

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  15. #15
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sneha24 View Post
    @silverbullet
    what is %s in "FROM_UNIXTIME(%s))UNIX_TIMESTAMP(dateOfLastLogin)"
    See sprintf().
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  16. #16
    SitePoint Addict
    Join Date
    Mar 2009
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $rResult mysql_query(
        
    sprintf(
            
    "SELECT TIMESTAMPDIFF(DAY, dateoflastlogin, FROM_UNIXTIME(%s))UNIX_TIMESTAMP(dateOfLastLogin) AS daysSinceLastLogin FROM profile WHERE username = '$username' LIMIT 1",
            
    time(),
            
    mysql_real_escape_string($username)
        )
    );
    $aRecord mysql_fetch_assoc($rResult);
    if(
    $aRecord['daysSinceLastLogin'] >= 1)
    {
        
    $sSQL sprintf(
            
    "UPDATE profile SET dateoflastlogin = FROM_UNIXTIME(%s), last_loginid = '$userid' WHERE username = '$username' LIMIT 1",
            
    time(),
            (Integer)
    $userid,
            
    mysql_real_escape_string($username)
        );
        
    $rResult mysql_query($sSQL);
    }
        
    //$ysec = 60*60*24*365 ; 
        //$hours = floor($diff / $ysec);
        //$t1=time();
        //echo $hours;
        
        
    elseif (($aRecord['daysSinceLastLogin'] < && $row2["last_loginid"]!=$userid))
        {
           
    header("Location: ../profile/error.php");
        } 
    its executing the else part

  17. #17
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, try this

    PHP Code:
    $sqlpage=mysql_query("SELECT TIMESTAMPDIFF(MINUTE, dateoflastlogin, NOW()) AS MinsSinceLastLogin FROM profile  WHERE username = '$username' ORDER BY MinsSinceLastLogin DESC LIMIT 0,5") 
     if(mysql_num_rows($sqlpage) > 0){

           while($row1 = $op->select($sqlpage)){
           $minutes_dif = $row1['MinsSinceLastVisit'];

    $minutes_dif = $row1['MinsSinceLastVisit'];
           $Ymin = 60 * 24 * 365;
           $Mmin = 60 * 24 * 30;
           $Wmin = 60 * 24 * 7;
           $Dmin = 60 * 24;
           $Hmin = 60;


           $Y = (int)($minutes_dif / $Ymin);
           $minutes_dif = $minutes_dif % $Ymin;

           $MON = (int)($minutes_dif / $Mmin);
           $minutes_dif = $minutes_dif % $Mmin;

           $W = (int)($minutes_dif / $Wmin);
           $minutes_dif = $minutes_dif % $Wmin;

           $D = (int)($minutes_dif / $Dmin);
           $minutes_dif = $minutes_dif % $Dmin;

           $H = (int)($minutes_dif / $Hmin);
           $minutes_dif = $minutes_dif % $Hmin;

            if($Y > 0 ){
                  echo "$Y year(s) ";

                  }else if($MON > 0){
                  echo "$MON month(s) ";
                  }
                  else if($W > 0){
                  echo "$W week(s) ";
                  }
                  else if($D > 0){
                  echo "$D day(s) ";
                  }
                  else if($H > 0){

                  echo "$H hour(s) ";
                  }


    else if($minutes_dif > 0) echo "$minutes_dif minute(s) ";
          echo "ago";   ?><? echo"\t";

                
                 }
                }


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
  •