Return to Snippet

Revision: 63797
at June 7, 2013 07:34 by vodou


Updated Code
/**
 * Takes one or two TIMESTAMPs, and an optional formatting array of the form ($year, $month, $day),
 * and returns a date that is appropriate to the situation
 * @param int $start
 * @param int $end
 * @param array $fmt
 * @return boolean|string
 */
function _pj_pretty_date( $start, $end = NULL, $fmt = NULL ) {
    if( ! isset( $start ) ) {
	return false;
    }
    
    if( ! isset( $fmt ) ) {
	// default formatting
	$fmt = array( 'Y', 'M', 'j' );
    }
    list( $yr, $mon, $day ) = $fmt;
    
    if( ! isset( $end) || $start == $end ) {
	return( date( "$mon $day, $yr", $start ) );
    }
    if( date( 'M-j-Y', $start ) == date( 'M-j-Y', $end ) ) {
	// close enough
	return date( "$mon $day, $yr", $start );
    }
    
    
    // ok, so $end != $start
		    
    // let's look at the YMD individually, and make a pretty string
    $dates = array( 
	's_year' => date( $yr, $start ),
	'e_year' => date( $yr, $end ),

	's_month' => date( $mon, $start ),
	'e_month' => date( $mon, $end ),

	's_day' => date( $day, $start ),
	'e_day' => date( $day, $end ),

    );
    // init dates
    $start_date = '';
    $end_date = '';

    $start_date .= $dates['s_month'];
    if( $dates['s_month'] != $dates['e_month'] ) {
	$end_date .= $dates['e_month'];
    }

    $start_date .= ' '. $dates['s_day'];
    if( $dates['s_day'] != $dates['e_day'] || $dates['s_month'] != $dates['e_month'] ) {
	$end_date .= ' ' . $dates['e_day'];
    }

    if( $dates['s_year'] != $dates['e_year'] ) {
	$start_date .= ', ' . $dates['s_year'];
	if( $dates['s_month'] == $dates['e_month'] ) {
	    if( $dates['s_day'] == $dates['e_day'] ) {
		// same day, same month, different year
		$end_date = ' ' . $dates['e_day'] . $end_date;
	    }
	    // same month, but a different year
	    
	    $end_date = $dates['e_month'] . $end_date;
	}
    }
    $end_date .= ', ' . $dates['e_year'];

    $complete_date = trim( $start_date ) . '–' . trim( $end_date );
    
    return $complete_date;
}

Revision: 63796
at June 7, 2013 07:19 by vodou


Initial Code
/**
 * Takes two TIMESTAMPs and returns a date that is appropriate to the situation
 * @param int $start
 * @param int $end
 * @return boolean|string
 */
function _pj_pretty_date( $start, $end = NULL ) {
    if( ! isset( $start ) ) {
	return false;
    }
    if( ! isset( $end) || $start == $end ) {
	return( date( 'M jS, Y', $start ) );
    }
    if( date( 'M-j-Y', $start ) == date( 'M-j-Y', $end ) ) {
	// close enough
	return date( 'M jS, Y', $start );
    }
    
    // ok, so $end != $start
		    
    // let's look at the YMD individually, and make a pretty string
    $dates = array( 
	's_year' => date( 'Y', $start ),
	'e_year' => date( 'Y', $end ),

	's_month' => date( 'M', $start ),
	'e_month' => date( 'M', $end ),

	's_day' => date( 'jS', $start ),
	'e_day' => date( 'jS', $end ),

    );
    // init dates
    $start_date = '';
    $end_date = '';

    $start_date .= $dates['s_month'];
    if( $dates['s_month'] != $dates['e_month'] ) {
	$end_date .= $dates['e_month'];
    }

    $start_date .= ' '. $dates['s_day'];
    if( $dates['s_day'] != $dates['e_day'] || $dates['s_month'] != $dates['e_month'] ) {
	$end_date .= ' ' . $dates['e_day'];
    }

    if( $dates['s_year'] != $dates['e_year'] ) {
	$start_date .= ', ' . $dates['s_year'];
	if( $dates['s_month'] == $dates['e_month'] ) {
	    if( $dates['s_day'] == $dates['e_day'] ) {
		// same day, same month, different year
		$end_date = ' ' . $dates['e_day'] . $end_date;
	    }
	    // same month, but a different year
	    
	    $end_date = $dates['e_month'] . $end_date;
	}
    }
    $end_date .= ', ' . $dates['e_year'];

    $complete_date = trim( $start_date ) . '–' . trim( $end_date );
    
    return $complete_date;
}

Initial URL

                                

Initial Description
Takes one or two PHP timestamps, and returns a somewhat humanized string representing the date range

* Jun 7th, 2013
* Jun 7th-11th, 2013
* Jun 7th-Jul 3rd, 2013
* Jun 7th, 2013-Jan 1st, 2013

Initial Title
PHP Date range formatter

Initial Tags
date

Initial Language
PHP