<?php

class Intense_Date extends Intense_Shortcode {

	function __construct() {
		parent::__construct();

		global $intense_visions_options;

		$this->title = __( 'Date', 'intense' );
		$this->description = __( 'Use the date shortcode anytime you want to display a date in a particular format.', 'intense' );
		$this->category = __( 'Typography', 'intense' );
		$this->show_preview = true;
	}

	function shortcode( $atts, $content = null ) {
		global $intense_visions_options;
		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		$useformat = '';
		$output = '';

		if ( $format == 'mdy_dash' ) { // - 03-10-2001
			$useformat = 'm-d-Y';
		} elseif ( $format == 'mdy_dot' ) { // - 03.10.2001
			$useformat = 'm.d.Y';
		} elseif ( $format == 'ymd_slash' ) { // - 2001/03/10
			$useformat = 'Y-m-d';
		} elseif ( $format == 'ymd_dash' ) { // - 2001-03-10
			$useformat = 'Y/m/d';
		} elseif ( $format == 'ymd_dot' ) { // - 2001.03.10
			$useformat = 'Y.m.d';
		} elseif ( $format == 'mdy' ) { // - March 10, 2001
			$useformat = 'F j, Y';
		} elseif ( $format == 'mdsy' ) { // - March 10th, 2001
			$useformat = 'F jS, Y';
		} elseif ( $format == 'jny' ) { // - 10, 3, 2001
			$useformat = 'j, n, Y';
		} elseif ( $format == 'dmj' ) { // - Sat Mar 10
			$useformat = 'D M j';
		} elseif ( $format == 'dmjy' ) { // - Sat Mar 10, 2001
			$useformat = 'D M j, Y';
		} elseif ( $format == 'dmjs' ) { // - Sat Mar 10th
			$useformat = 'D M jS';
		} elseif ( $format == 'dmjsy' ) { // - Sat Mar 10th, 2001
			$useformat = 'D M jS, Y';
		} elseif ( $format == 'lmj' ) { // - Saturday Mar 10
			$useformat = 'l M j';
		} elseif ( $format == 'lmjy' ) { // - Saturday Mar 10, 2001
			$useformat = 'l M j, Y';
		} elseif ( $format == 'lmjs' ) { // - Saturday Mar 10th
			$useformat = 'l M jS';
		} elseif ( $format == 'lmjsy' ) { // - Saturday Mar 10th, 2001
			$useformat = 'l M jS, Y';
		} elseif ( $format == 'l' ) { // - Saturday
			$useformat = 'l';
		} else if ( $format == 'fuzzy' ) {
			if ( empty( $date ) ) $date = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
			return Intense_Date::get_relative_time( strtotime( $date ) );			
		} else { //mdy_slash - 03/10/2001
			$useformat = 'm/d/Y';
		}
		
		if ( empty( $date ) ) return date( $useformat );

		if ( (bool)strtotime( $date ) && date("m/d/Y", strtotime( $date ) ) == $date ) {
			$output .= date( $useformat, strtotime( $date ) );
		} else {
			$output = $date;
		}		

		return $output;
	}

	public static function get_relative_time( $time, $gmt = 0 ) {
		$periods  = array( 
			__( 'second', 'intense' ), 
			__( 'minute', 'intense' ), 
			__( 'hour', 'intense' ), 
			__( 'day', 'intense' ), 
			__( 'week', 'intense' ), 
			__( 'month', 'intense' ), 
			__( 'year', 'intense' ), 
			__( 'decade', 'intense' ) 
		);

		$periods_plural = array( 
			__( 'seconds', 'intense' ), 
			__( 'minutes', 'intense' ), 
			__( 'hours', 'intense' ), 
			__( 'days', 'intense' ), 
			__( 'weeks', 'intense' ), 
			__( 'months', 'intense' ), 
			__( 'years', 'intense' ), 
			__( 'decades', 'intense' ) 
		);

		$period_lengths = array( 
			'60', //seconds
			'60', //minutes
			'24', //hours
			'7', //days
			'4.35', //months
			'12', //years
			'10' //decades
		);

		$now = current_time( 'timestamp', $gmt );
		$difference = $now - $time;
		$suffix = __( 'ago', 'intense' );

		if ( $difference < 0 ) {
			$suffix = __( 'from now', 'intense' );
			$difference *= -1;
		}		

		for ( $i = 0; $difference >= $period_lengths[ $i ] && $i < count( $period_lengths ) - 1; $i++ ) {
			$difference /= $period_lengths[ $i ];
		}

		$difference = round( $difference );

		if ( $difference != 1 ) {
			$periods[ $i ] = $periods_plural[ $i ];
		}

		return sprintf( '%s %s %s', $difference, $periods[ $i ], $suffix );
	}
}
