Keep Cookies

You'll have to set a fixed value for time & path.

If you have created any cookies, this function can recreate them, so that cookies won't disappear as long as the user visits your website before the expiring time.

Getting the time & path from a cookie isn't possible so you'll have to set fixed values.

The line if (key($_COOKIE) !== 'PHPSESSID') { tests if the cookie isn't the session. This line is only necessary if you use session.

<?php
	if (count($_COOKIE) > 0) {
		while ($cookieVal = current($_COOKIE)) {
			if (key($_COOKIE) !== 'PHPSESSID') {
				setcookie(key($_COOKIE), $cookieVal, time()+86400, "/");
			}
			
			next($_COOKIE);
		}
	}
?>

Calculate human readable time

This function returns a human readable time (for example for a chat). As a parameter it takes a date-time string that lies in the past.

The function can return the following:

  1. Less than 10 seconds: Now
  2. Less than 1 minute: s second(s) ago
  3. Less than 1 hour: i minute(s) ago
  4. Same day, more than 1 hour: Today - H:i
  5. Yesterday: Yesterday - H:i
  6. Other: d M Y - H:i

The meaning of the given date-time placeholders can be found here: www.php.net - date.

function calcTime($datetimeStr) {
	$timeStr = "";
	$now = date_create();
	$datetime = date_create($datetimeStr);
	
	if ($datetime->format("Y-m-d") == $now->format("Y-m-d")) { /* Today */
		if (date_diff($datetime, $now)->h == 0) { /* This hour */
			if (date_diff($datetime, $now)->i == 0) { /* This minute */
				if (date_diff($datetime, $now)->s <= 10) { /* Less than 10 sec */
					$timeStr = "Now";
				} else {
					$timeStr = date_diff($datetime, $now)->s . " second(s) ago";
				}
			} else {
				$timeStr = date_diff($datetime, $now)->i . " minute(s) ago";
			}
		} else {
			$timeStr = "Today - " . date_format($datetime, "H:i");
		}
	} else if ($datetime->format("Y-m-d") == $now->modify("-1 day")->format("Y-m-d")) { /* Yesterday */
		$timeStr = "Yesterday - " . date_format($datetime, "H:i");
	} else { /* Other days */
		$timeStr = date_format($datetime, "d M Y - H:i");
	}
	
	return $timeStr;
}

My Website

Cookie Usage

This website uses Cookies to help you use the website faster.

My Website Logo