[Date Prev][Date Next][Thread Prev][Thread Next][Minivend by date
][Minivend by thread
]
Re: [mv] date/time calculation tag?
****** message to minivend-users from "Bill Randle" <billr@exgate.tek.com> ******
****** message to minivend-users from jojo@buchonline.net ******
> On Apr 28, 10:06pm, Shozo Murahashi wrote:
} > Subject: [mv] date/time calculation tag?
} > ****** message to minivend-users from "Shozo Murahashi"
<murahashi@ayayu.com> ******
} >
} > HI all,
} >
} > I would like to calculate the difference between dates.
} > For example, the expiration date of the product's available.
} > I mean that product catalog shows "This product expires in 30 days".
} >
} > Is there this kind of MV tags or perl codes such as the follows?
} > Life time = Expire_date - Current_time
} > Expire_date = Begin_date + Life_time
} >
} > What is the best way to this? Any suggention would be helpful.
}
} Do you mean this?
}
} ---------snipp--------
} #!/usr/bin/perl
} use Time::localtime;
} use Time::Local;
} my $expiredate = localtime(timelocal(00, 00, 00 , 10, 05, 2000));
} $expiredate = $expiredate->yday;
} print "expiredate : " . $expiredate . "\n";
} my $current_time = localtime;
} $current_time = $current_time->yday;
} print "current_time : " . $current_time . "\n";
}
} my $lifetime = $expiredate - $current_time;
} print "lifetime : " . $lifetime . "\n";
}
} print "This product expires in " . $lifetime . " !\n";
} ---------snipp--------
The above script doesn't work for me, and I think, will have problems
at the end of the year when 'yday' wraps around.
What you use depends on how you have the date information stored
away and what you are actually putting in your products file.
Typically, the begin date is stored in YYYYmmddHHMM format,
which makes it difficult to do these kinds of comparisons unless
you convert it back into time() format (seconds since 1/1/1970).
There's a usertag included in mv4 that converts the current time
to a formatted time, so I created my own usertag that does the
inverse - i.e. calculates seconds given a formatted time. (There
may be something in MV that does this already, but I haven't
run across it.)
# convert a time string to an actual time() value
# i.e. the inverse of strftime('%Y%m%d%H%M')
UserTag timestr Order str
UserTag timestr PosNumber 1
UserTag timestr Routine <<EOR
sub {
use Time::Local;
my ($str) = @_;
my ($year, $mon, $day, $hr, $min, $sec) = unpack "a4a2a2a2a2a2", $str;
$sec = $sec || 0;
$min = $min || 0;
$hr = $hr || 0;
# convert to struct tm format
$year -= 1900;
$mon--;
my $timeval = timelocal($sec, $min, $hr, $day, $mon, $year);
return $timeval;
}
EOR
Now, using perl or mvasp, you can do something like:
[seti code][item-code][/seti]
[mvasp]
<%
my $lifetime = 30*24*60*60; # expiration time in seconds
my $curtime = time();
my $stime_fmt = tag_data('products', 'begin', $Scratch->{code});
my $stime = $Tag->timestr($stime_fmt);
my $remtime = $stime + $lifetime - $curtime;
$remtime = int $remtime/60; # ignore seconds and truncate
if ($remtime > 0) {
my $edays = int $remtime / (24 * 60);
$remtime -= $edays * 24 * 60;
my $ehrs = int $remtime / 60;
my $emin = $remtime - $ehrs * 60;
my $out;
if ($edays) {
$out = sprintf '%d days and %d:%02d', $edays, $ehrs, $emin;
}
else {
$out = sprintf '%d:%02d', $ehrs, $emin;
}
HTML("<FONT COLOR='RED'>Only $out remaining on this sale!</FONT>");
}
else {
HTML("Sorry, this item has expired.");
}
%>
[/mvasp]
-Bill Randle
billr@exgate.tek.com
-
To unsubscribe from the list, DO NOT REPLY to this message. Instead, send
email with 'UNSUBSCRIBE minivend-users' in the body to Majordomo@minivend.com.
Archive of past messages: http://www.minivend.com/minivend/minivend-list