[pmwiki-users] How to: calculate how many days left

Hans design5 at softflow.co.uk
Tue Feb 26 05:08:37 CST 2008


Tuesday, February 26, 2008, 9:51:51 AM, Hans wrote:

> {(sub (ftime %j {$:Date}) (ftime %j now) )}

Note that %j formats the output as a "day in the year" number.
So calculations across different years are wrong.

To overcome this we can use unix time periods using the PHP date()
function. (I could not see a unix time format returned by strftime())

The following markup expression will give a unix timestamp
(seconds from the unix epoch 1-Jan-1970 00:00 UTC), the last one will
give the days from the unix epoch, time is rounded down.
Both accept 'now' as a parameter.

# unix time, seconds since 1970-01-01
$MarkupExpr['utime'] = 'MxUTime($args[0])';
function MxUTime($arg) {
        $udate = date('U', strtotime($arg));
        if ($arg=='now') $udate = time();
        return $udate;
}

# unix time, days since 1970-01-01
$MarkupExpr['udays'] = 'MxUDays($args[0])';
function MxUDays($arg) {
        $udate = date('U', strtotime($arg));
        if ($arg=='now') $udate = time();
        return floor($udate/86400);
}

So  the days left could be written as

 {(sub (udays {$:Date}) (udays now) )}


  ~Hans




More information about the pmwiki-users mailing list