www.webdeveloper.com
Results 1 to 9 of 9

Thread: [RESOLVED] Need help with dates

  1. #1
    Join Date
    Aug 2005
    Location
    USA
    Posts
    116

    resolved [RESOLVED] Need help with dates

    Hey everyone,

    I have a relativly simple problem, but I am not very strong in php, so I'm having some diffculties getting this to work.

    I have a DB that holds a membership registration date and I need to compare that against the current date to tell them how many days they have left on thier membership. But, I can't get it work.

    Here is some of what I have.
    Code:
    $memdate = mysql_result($memberdate, "membershipdate"); //pulls date from DB
    $d1 = getdate($memdate);
    $d2 = date("Y-m-d");
    $_SESSION['memberdate'] = $d2['yday']-$d1['yday']; //amount of days between DB date & now
    I know thats obviously not right, but could someone tell me what I'm doing wrong? The field in the MySQL db is setup as DATE if that helps. Does it read and compare these as strings?

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Jul 2006
    Location
    Nebraska
    Posts
    380
    There may be a better way, I'm not familiar enough with SQL, but this ought to work.

    PHP Code:
    $memdate mysql_result($memberdate"membershipdate"); //pulls date from DB
    $dbtimestamp mktime(0,0,0,substr($memdate,0,4),substr($memdate,5,2),substr($memdate,8,2));
    $difference time() - $dbtimestamp;
    $days $difference / (60*60*24);
    print 
    "It has been $days days."
    I'm used to using the unix epoch, so thats how I did it.

  3. #3
    Join Date
    Jul 2006
    Location
    Nebraska
    Posts
    380
    Ack! That's not right, I got the year out of place on it, swap this in instead:
    PHP Code:
    $dbtimestamp mktime(0,0,0,substr($memdate,5,2),substr($memdate,8,2),substr($memdate,0,4)); 

  4. #4
    Join Date
    Aug 2006
    Location
    Netherlands
    Posts
    71
    Leaving your SQL statement out of it (I cannot determine if that is ok).
    The following should do better:
    PHP Code:
    $d1 getdate($memdate);                   // return day of year in $d1['yday']
    $d2 date("z");                           // return day of year
    $_SESSION['memberdate'] = $d2-$d1['yday']; //amount of days between DB date & now 
    Ronald

  5. #5
    Join Date
    Jul 2006
    Location
    Nebraska
    Posts
    380
    Okay, I wasn't really thinking there. You can just use strtotime()
    PHP Code:
     $memdate mysql_result($memberdate"membershipdate"); //pulls date from DB
    $dbtimestamp =strtotime($memdate);
    $difference time() - $dbtimestamp;
    $days $difference / (60*60*24);
    print 
    "It has been $days days."

  6. #6
    Join Date
    Jul 2006
    Location
    Nebraska
    Posts
    380
    roverdonk: I don't think the mysql date format is a time stamp, so getdate() isn't going to do anything for it.

  7. #7
    Join Date
    Aug 2005
    Location
    USA
    Posts
    116
    Ok, well let me include a little more info, because it works, but it's not returning the correct amount of days.

    the SQL call is as follows:
    Code:
    $memberdate = mysql_query("SELECT membershipdate FROM users WHERE username='$username' AND password='$password'");
    The particular user that I'm using established his membership on 2006-08-24

    But when the code runs, it returns -123 for the days left on his membership.
    I might be wrong here, but to me, it almost seems like thats taking the total number of days in the year and subtracting it minus how many days were into the year already.

  8. #8
    Join Date
    Aug 2005
    Location
    USA
    Posts
    116
    Quote Originally Posted by PineSolPirate
    Okay, I wasn't really thinking there. You can just use strtotime()
    PHP Code:
     $memdate mysql_result($memberdate"membershipdate"); //pulls date from DB
    $dbtimestamp =strtotime($memdate);
    $difference time() - $dbtimestamp;
    $days $difference / (60*60*24);
    print 
    "It has been $days days."
    Hey thanks, that worked!! But one small thing, it returns the days in a float, ie. like 5.434343456. How can I round that down to the right integer?

  9. #9
    Join Date
    Aug 2005
    Location
    USA
    Posts
    116
    Ignore that last post, I got it. Used intval.

    Thanks again for all the help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles