Skip to content

Instantly share code, notes, and snippets.

@RShergold
Last active August 29, 2015 14:05
Show Gist options
  • Save RShergold/de967a4390a9c17e81e3 to your computer and use it in GitHub Desktop.
Save RShergold/de967a4390a9c17e81e3 to your computer and use it in GitHub Desktop.
<?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