PHP – Check If File Age In Minutes and Perform Action

Had a need for this logic today – wrote up this snippet.

if(file_exists($filePath)) {
	$lastEdit = filemtime($filePath);
	$lastEditReadable = gmdate('r', $lastEdit);

	$currentTime = time();
	$currentTimeReadable = gmdate('r', $currentTime);

	$ageOfFileInSeconds = $currentTime - $lastEdit;
	$ageOfFileInMinutes = $ageOfFileInSeconds / 60;

	if($ageOfFileInMinutes < 10)
	{
		// Do Thing
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s