Location: PHPKode > scripts > Fortnight > fortnight/fortnight.class.php
<?php
/*
================================================================================
							fortnight.class.php
--------------------------------------------------------------------------------
Creation: 2005/04/06			   Author: Rafael Costa <hide@address.com>
--------------------------------------------------------------------------------
Description:
	Simple class to find out the fortnight number in a year.
	
	That is calculated based on the number of days from January 1st, and not
	only a simple checking if the date is between days 1 to 15 or 16 to 31 and
	then multiplying by the month number.

Instructions:
	Just pass a date in the format yyyy/mm/dd or a timestamp to get the
	fortnight. Just like this:
	
	$fnight = new Fortnight ("2005/04/06");
	echo $fnight->TellFortnight (); // prints 7
================================================================================
*/


/*
 *
 * The class; see instructions and description in the
 * header.
 *
 * Return the fortnight number of the week or false in
 * case of invalid date input
 *
 */
class Fortnight {

	var $timestamp;	// the timestamp

	/*
	 * Constructor
	 *
	 * @param (mixed) date: a timestamp or a string
	 *				representing a yyyy/mm/dd date
	 */	
	function Fortnight ($date)
	{
		// Checks if it's a timestamp
		if (is_int ($date))
			$this->timestamp = $date;
		// Now checks if it is a date in yyyy/mm/dd format
		else {
			// Before exploding the date, do some rudementary
			// input validation
			if (preg_match ("/^[\d]{4}\/([0]?\d|1[0-2])\/([1-2][0-9]|3[0-1]|[0]?[1-9])$/", $date)) {
				$temp = explode ("/", $date);
			
				if (checkdate ($temp [1], $temp [2], $temp [0]))
					$this->timestamp = mktime (0, 0, 0, $temp [1], $temp [2], $temp [0]);
			}
		}
	}


	/*
	 * TellFortnight() : returns the fortnight number
     * of the year from the timestamp or 0 if date
	 *
	 * @return (int) fortnight : fortnight number of the year
	 *				or false if invalid date
	 */
	function TellFortnight () {
		if (isset ($this->timestamp))
			$tstamp = $this->timestamp;
		else
			return (false);

		// Day of the year
		$day	= date ("z", $tstamp) + 1;
		// Week of the year
		$week	= ($day >= 7 ? ceil ($day / 7) : 1);

		return ((($week % 4) % 2) == 1 ? ($week + 1) / 2 : $week / 2);
	}

} // End of class Fortnight

?>
Return current item: Fortnight