SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast bobbobbobbob's Avatar
    Join Date
    Apr 2004
    Location
    bobbobbobbob
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding months to a php date function

    I have a page that Im creating and I want to be able to activate profiles for three months and then is deactivated.

    PHP Code:
    echo date('m-d-Y'); 
    I use that to get the current date, but how can I add three months to that and then deactivate the 'activated' field in my Mysql database when the time runs out?

    Thanks,
    zack

  2. #2
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My suggestion would be to store the create date (only), then run a process (perhaps using CRON) to look for entries where create date is greater than three months old.

  3. #3
    Non-Member
    Join Date
    Apr 2006
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you can do is add the date when a profile was created to your mysql database. You can create a new field in your profile table and assign it to the value of NOW() in your SQL statement that adds a new profile.

    Then, you can pull the profile creation date and substract it from the current date to see if 3 months have passed. There are many date difference functions that are written in PHP that you can find here.

  4. #4
    SitePoint Enthusiast bobbobbobbob's Avatar
    Join Date
    Apr 2004
    Location
    bobbobbobbob
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT:::Thanks a lot abayle, I think I'll try that out.

  5. #5
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    London
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's one way to get "three months ago" using timestamps:

    PHP Code:
    echo '<p>Current timestamp: 'time();

    //three months ago
    $past=mktime(0,0,0,date('m')-3,date('d'),date('y'));

    echo 
    '<p>Three months ago' $past;
    echo 
    '<p>Three months ago' date('d-m-Y',$past); 

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As has been said, you can easily do the comparison from the database. It's more efficient than comparing the dates in PHP.

    Code:
    SELECT * FROM table WHERE create_date < date_sub(current_timestamp, interval 3 month)


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
  •