SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: compair dates

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

    compair dates

    If have the following problem:

    - I have a database table with user-information. When the power-user deletes the User, the tablerow 'date_delete' is set to:
    PHP Code:
    $date    date('Y-m-d'); 
    - What I want to do, is write a function that automatically deletes the user after three months upon deletion.

    So if the date in tablerow date_delete is three months old, I want to execute an Delete query.

    Now, the delete-query isn't the problem. But HOW do I compair two dates with each other? Especially when the user is deleted in december (m = 12) and the row has to be deleted in march.

    Please advise, any help is appreciated!

    Jazz

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had to do something like this.. It's easier to use unixtime instead of formatted time for this kind of stuff.. in my opinion.

    So instead of setting 'date_delete' to date('Y-m-d') do this:

    PHP Code:

    $date 
    mktime()+7257600;    /* there are approximately 7257600 seconds in 3 months */

    /* do whatever you have to do here to insert $date
       into 'date_delete' */



    /*
    Delete function would look something like this.
    this deletes all entries where the current unixtime is
    less than date_delete. Since we set date_delete 3
    months in advance, it will delete these entries.
    */

    function delete_query() {
       
    mysql_query("delete from table_name where date_delete<".mktime());

    Last edited by cupid; Sep 5, 2001 at 18:55.

  3. #3
    SitePoint Addict lveale's Avatar
    Join Date
    Jun 2001
    Location
    Dublin
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a function I used recently. It expects dates in dd/mm/yyyy format but could easily be changed.



    PHP Code:


    function compareDate ($i_sFirstDate$i_sSecondDate)
    {
    /*
     * Break the Date string into seperate components
     */
          
                    
                    
    if(isDate($i_sFirstDate) && isDate($i_sSecondDate))
    {
     
    $arrFirstDate explode ("/"$i_sFirstDate);
     
    $arrSecondDate explode ("/"$i_sSecondDate);

     
    $intFirstDay    $arrFirstDate[0];
     
    $intFirstMonth  $arrFirstDate[1];
     
    $intFirstYear   $arrFirstDate[2];

     
    $intSecondDay   $arrSecondDate[0];
     
    $intSecondMonth $arrSecondDate[1]; 
     
    $intSecondYear  $arrSecondDate[2];


     
    /* 
    Calculate the diference of the two dates and return the number of days.
         
    Returns <0, 0, >0 if a<b, a==b, a>b respectively.
    */
                          
    $intDate1Jul gregoriantojd($intFirstMonth$intFirstDay$intFirstYear);

    $intDate2Jul gregoriantojd($intSecondMonth$intSecondDay$intSecondYear);
                
    return     
    $intDate1Jul $intDate2Jul;
    }

    function 
    isDate($i_sDate)
    {
    $blnValid TRUE;

    if(!
    ereg ("^[0-9]{2}/[0-9]{2}/[0-9]{4}$"$i_sDate))
    {
      
    $blnValid FALSE;
    }
    else 
    //format is okay, check that days, months, years are okay
    {
    $arrDate explode("/"$i_sDate); // break up date by slash
    $intDay $arrDate[0];
    $intMonth $arrDate[1];
    $intYear $arrDate[2];

    $intIsDate checkdate($intMonth$intDay$intYear);
        
    if(!
    $intIsDate)
    {
      
    $blnValid FALSE;
    }

    }
    //end else
       
    return ($blnValid);
    //end function isDate 
    Last edited by lveale; Sep 5, 2001 at 22:38.

  4. #4
    SitePoint Member
    Join Date
    Jul 2001
    Location
    Montréal, Québec
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you using a mysql db? if you do, I just found the sub_date() and add_date functions and it works great!
    ...gravity always wins...


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
  •