Last active
August 29, 2015 14:05
-
-
Save RShergold/de967a4390a9c17e81e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
A simple friendly date parser for upcoming dates | |
in the near future. | |
*/ | |
function friendly_date( $date_string ) { | |
$date = new DateTime( $date_string ); | |
$today = new DateTime(); | |
// is the date today? | |
if ( $date->format('Ymd') == $today->format('Ymd') ) { | |
return 'Today'; | |
} | |
// is the date tomorrow? | |
$tomorrow = new DateTime('tomorrow'); | |
if ( $date->format('Ymd') == $tomorrow->format('Ymd') ) { | |
return 'Tomorrow'; | |
} | |
// days this week | |
if ( $date->format('W') == $today->format('W')) { | |
return 'This ' . $date->format('l'); | |
} | |
// days next week | |
$next_week_number = $today->format('W') + 1; | |
if ( $date->format('W') == $next_week_number ) { | |
return 'Next ' . $date->format('l'); | |
} | |
// later | |
return $date->format('l \t\h\e jS \of F'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment