Jump to content

[SOLVED] Problem with date 2009


gabrielp

Recommended Posts

Hi,

 

I have been using the following code all 2008 , it  checks if user is active, check last activation date, number of months paid, add that time to the last activation date and also check if date is grater than actual date.

 

It worked great until Jan 1st, when I guess there's a problem with users with active accounts on 2008 and months calculation with 2009

 

<?php
$myrow = mysql_fetch_array($result);
                    
$date_activation = $myrow['start_date'];
$months = $myrow['type'];

$date_now = date("Y-m-d h:m:s", time());
$qty_years=floor($months/12);
$qty_months=$months%12;

$bits=explode("-",$date_activation);
$year=$bits[0];                     
$month = sprintf('%02d',$bits[1]);
$newyear=$year+$qty_years;
$newmonth=sprintf('%02d', $month+$qty_months);

$date_expires=$newyear."-".$newmonth."-".$bits[2];

if ($date_now <= $date_expires) 
    
    {
    
                // user is active, write a cookie and say welcome
                
    }  else {
    
    // subscription expired
        

    }
?>

 

Thanks for all the help that you can provide!

 

(edited to change tags to


tags)

Link to comment
https://forums.phpfreaks.com/topic/139215-solved-problem-with-date-2009/
Share on other sites

Why don't you just use the strotime function to do the figuring?

<?php
$date_term = strtotime($myrow['start_date'] . ' + ' . $myrow['type'] . ' months');
if (time() <= $date_term) {
//
//   ok
//
} else {
//
// not ok
//
}
?>

 

Ken

$myrow = mysql_fetch_array($result);
                    
$date_activation = $myrow['start_date'];
$months = $myrow['type'];

$timestamp_activation = strtotime($date_activation);
$timestamp_type = strtotime("$months months ago");
if ($timestamp_activation < $timestamp_type) {
  //subscription expired
} 

That's a pretty nasty way of checking if they're active or not. I'd do much more of the work with mysql:

(Edit: Comment wasn't directed at you Mchl or Ken; but i'd still do the work with mysql.)

 

$sql = "SELECT COUNT(*) FROM yourtable WHERE username='$username' AND start_date + INTERVAL type MONTH >= CURDATE()";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
if(mysql_resut($result,0) > 0){
//active subscription
}else{
//subscription ended.
}

 

Im assuming that you're start_date field has type DATE, your type field contains the number of months of their subscription and you have a field called username

Thank you all guys !!!! I finally used

 

 

$myrow = mysql_fetch_array($result);

                   

$date_activation = $myrow['start_date'];

$months = $myrow['type'];

 

$timestamp_activation = strtotime($date_activation);

$timestamp_type = strtotime("$months months ago");

if ($timestamp_activation < $timestamp_type) {

  //subscription expired

}

 

but I will try the mySql way too :)

 

Thanks!!

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.