I know this isn't specifically a contemplate issue - but...

I've built an events listing view and I'm trying to print a statement in the teaser using contemplate that will give the days left before an event closes. I've been trying to adapt this snippet (used to calculate user membership expiry dates:http://drupal.org/node/36123)

$date_key = 'profile_expiry_date'; /* change the name to the profile date field you are using */
if ((user_access('administer users')) || ($GLOBALS['user']->uid == $user->uid)):

$keyMonth = $user->$date_key{month};
$keyDay = $user->$date_key{day};
$keyYear = $user->$date_key{year};
$month = date(F); $mon = date(n); $day = date(j); $year = date(Y);
$hours_left = (mktime(0,0,0,$keyMonth,$keyDay,$keyYear) - time())/3600;
$daysLeft = ceil($hours_left/24);
$z = (string)$daysLeft;
if ($z > 1):

There are print $z ; days left until your membership expires with this site

endif;
endif;

But I'm not having any luck. I think this might be because I'm using 'datestamp' not 'date' fields.

Anybody have any ideas on this?

Comments

jrglasgow’s picture

The main question is this: When you are looking at the data, what format is the date in?

When doing this type of date calculation you need to get the date into a unix time stamp If the time is in the format "2010-07-15 07:58:23 -0700" (my current time) the to get it to the current unix timestamp

$timestamp = strtotime("2010-07-15 07:58:23 -0700");

then you can do your calculation

$difference = time() - $timestamp; // this will give you the number of seconds difference

With the number of seconds difference you should be able to figure out the number of minuts/hours/days/ etc...

plan9’s picture

OK - found the secret. If you are using datestamp you have to include a conversion function to your statements: date('m-d-Y',$timeStamp)

http://drupal.org/node/335698

I should have posted this in the date modules queue... but maybe it will be useful for someone.