add new function getTsByPeriod()

This commit is contained in:
Uwe Steinmann 2023-09-29 14:29:30 +02:00
parent 60410708de
commit 70a455c422

View File

@ -166,6 +166,79 @@ function getReadableDurationArray($secs) { /* {{{ */
return $units;
} /* }}} */
/**
* Return the timestamp after a period of time
*
* The period is relative to $start. $r sets if the returned
* time stamp is the start, end or current seconds of day.
* getTsByPeriod('today', 's') returns 00:00:00 of today
* getTsByPeriod('tomorrow', 'e') returns 23:59:59 of tommorrow
* The parameter $start can be any timestamp from where the
* period starts
* getTsByPeriod('today', 's', time()+7*86400) is equivalent to
* getTsByPeriod('1w', 's')
*/
function getTsByPeriod($period, $r='', $start=0) { /* {{{ */
if(!$start)
$start = time();
$sofd = time() - strtotime('today'); // seconds elapsed today
switch($r) {
case 's': // start of day
$o = $sofd;
break;
case 'e': // end of day
$o = $sofd-86400+1;
break;
default: // same sec as current time
$o = 0;
}
switch($period) {
case "never":
$expiration = 0;
break;
case "1h":
$expiration = $start+3600;
break;
case "2h":
$expiration = $start+7200;
break;
case "24h":
$expiration = $start+86400;
break;
case "today":
$expiration = $start - $o;
break;
case "1d":
$expiration = $start-$o+86400;
break;
case "tomorrow":
$expiration = $start - $o + 86400;
break;
case "1w":
$expiration = $start-$o+7*86400;
break;
case "1m":
$tmp = explode('-', date('Y-m-d', $start));
$expiration = mktime(0,0,0, $tmp[1]+1, $tmp[2], $tmp[0])+$sofd-$o;
break;
case "1y":
$tmp = explode('-', date('Y-m-d', $start));
$expiration = mktime(0,0,0, $tmp[1], $tmp[2], $tmp[0]+1)+$sofd-$o;
break;
case "2y":
$tmp = explode('-', date('Y-m-d', $start));
$expiration = mktime(0,0,0, $tmp[1], $tmp[2], $tmp[0]+2)+$sofd-$o;
break;
case "3y":
$tmp = explode('-', date('Y-m-d', $start));
$expiration = mktime(0,0,0, $tmp[1], $tmp[2], $tmp[0]+3)+$sofd-$o;
break;
default:
$expiration = false;
}
return $expiration;
} /* }}} */
/* Deprecated, do not use anymore, but keep it for upgrading
* older versions
*/