Snippet: Time class for PHP

Pretty boring snippet today, but I find it immensely useful. My time class provides representations of periods from OneMinute to OneYear. It only has one method which will calculate an absolute time by adding a given time period to the current time. You can optionally provide a format intended for use by the date function to return a formatted string rather than a timestamp.

Hope you find it useful too.

	class Time
	{
		const OneMinute = 60;
		const FiveMinutes = 300;
		const TenMinutes = 600;
		const FifteenMinutes = 900;
		const HalfHour = 1800;
		const OneHour = 3600;
		const SixHours = 21600;
		const HalfDay = 43200;
		const OneDay = 86400;
		const SevenDays = 604800;
		const ThirtyDays = 2592000;
		const OneYear = 31536000;

		public static function GetAbsolute($time, $format = false)
		{
			if (is_numeric($time) and $time < (time()-1))
			{
				$time = time() + $time;
			}
			else
			{
				$time = strtotime($time);
			}
			return (false === $format ? $time : date($format, $time));
		}
	}

Comments, questions, suggestions and requests are welcomed as always.

Oct 17th, 2008 | Posted in Misc
Tags:
  1. Oct 17th, 2008 at 18:40 | #1

    I can definitely attest to the usefulness of this class, having used it before!

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>