list check expiry date for calendar

Discussion in 'PHP' started by s0fi3a, Jun 8, 2010.

  1. #1
    i've been trying to find out how to calculate for expiry date in php.
    basically i've 2 date signup_date and expiry_date. user have to key in singup_date but for expiry_date i wanted to come out calculating by 3month, 6month, 1year and 2year etc. i dunno how to calculate for those using php.
    i found out this one
    but that was not working.
     
    s0fi3a, Jun 8, 2010 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    something like this
    
    $years = 1;
    $months = 6;
    $days = ($years * 365) + ($months * 30);
    // or
    $days = ($years * 365) + (365/$months);
    
    $expiry_date= time() + (
        $days * 
        24 /* hours */ *
        60 /* minutes */ *
        60 /* seconds */
    
    );
    
    PHP:
     
    Last edited: Jun 8, 2010
    gapz101, Jun 8, 2010 IP
  3. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Rather use date and mktime (see below) with php lesser than 5.3

    With php 5.3 and up you can use "Datetime" and "Dateinterval"

    
    // Example for php < 5.3
    echo "Today: " 
    .date('Y-m-d') ."\n";
    
    $nMonths = 2;
    echo "2 Month ahead: " 
    .date('Y-m-d', 
        mktime(
        	0,
      		0, 
      		0, 
      		date('m',strtotime(date('Y-m-d')))+$nMonths
     	)
    ) . "\n";
    
    
    $nYears = 2;
    echo "2 Years ahead: "
    .date('Y-m-d', 
        mktime(
        	0,
      		0, 
      		0, 
      		date('m',strtotime(date('Y-m-d'))),
      		date('d',strtotime(date('Y-m-d'))),
      		date('Y',strtotime(date('Y-m-d')))+$nYears
     	)
    ) . "\n";
    
    PHP:
    A Ready to use function for php < 5.3 from:
    http://de3.php.net/manual/en/function.date-add.php#95574
    
    function add_date($givendate,$day=0,$mth=0,$yr=0) {
        $cd = strtotime($givendate);
        $newdate = date('Y-m-d h:i:s', mktime(date('h',$cd),
        date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
        date('d',$cd)+$day, date('Y',$cd)+$yr));
        return $newdate;
    }
    
    // 2 month
    echo dateAdd(date('Y-m-d'), 0, 2, 0);
    
    // 2 years
    echo dateAdd(date('Y-m-d'), 0, 0, 2);
    
    PHP:
     
    flexdex, Jun 8, 2010 IP