Skip to content

Instantly share code, notes, and snippets.

@oropesa
Created December 7, 2018 00:05
Show Gist options
  • Save oropesa/07ac6f0532cb4f376b1ec03f0c38034a to your computer and use it in GitHub Desktop.
Save oropesa/07ac6f0532cb4f376b1ec03f0c38034a to your computer and use it in GitHub Desktop.
/**
* In: . Out: 2018
* In: 2018. Out: 2018
* In: 2013. Out: 2013-18
* In: 1998. Out: 1998-2018
*
* @param string $year
* @return string
*/
function get_website_year_start( $year ) {
switch( true ) {
case empty( $year ): //no year
case intval( $year ) == intval( date( 'Y' ) ): //same year
$html = date( 'Y' );
break;
case substr( strval( $year ), 0, 2 ) === substr( strval( date( 'Y' ) ), 0, 2 ): //same century
$html = $year . '-' . substr( strval( date( 'Y' ) ), 2, 2 );
break;
//diff years
default:
$html = $year . '-' . date( 'Y' );
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment