Is older than

Submitted by:Pitter Alderson

Date added:14 June, 2015

Category:PHP

This function helps you checking a UNIX timestamp with human readable time formats like 1m, 2d or 4y.

Tags: php function , timestamp

Code Snippet:

function time_is_older_than($t, $check_time){

$t = strtolower($t);
$time_type = substr(preg_replace('/[^a-z]/', '', $t), 0, 1);
$val = intval(preg_replace('/[^0-9]/', '', $t));
$ts = 0;

// (s)econds, (m)inutes, (d)ays, (y)ears
if ($time_type == 's'){ $ts = $val; }
else if ($time_type == 'm'){ $ts = $val * 60; }
else if ($time_type == 'h'){ $ts = $val * 60 * 60; }
else if ($time_type == 'd'){ $ts = $val * 60 * 60 * 24; }
else if ($time_type == 'y'){ $ts = $val * 60 * 60 * 24 * 365; }
else { die('Unknown time format given!'); }

if ($check_time < (time()-$ts)){ return true; }

return false;
}
 
 

Comments