Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tomnomnom
Created July 27, 2013 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomnomnom/6095481 to your computer and use it in GitHub Desktop.
Save tomnomnom/6095481 to your computer and use it in GitHub Desktop.
<?php
class SayDate extends DateTime {
public function say(){
$now = new DateTime();
$now->setTime(0, 0, 0);
$interval = (int) $now->diff($this)->format('%r%a');
if ($interval < 0){
return $this->format('Y-m-d');
}
if ($interval == 0){
return 'Today';
}
if ($interval == 1){
return 'Tomorrow';
}
$weekInterval = $this->format('W') - $now->format('W');
if ($weekInterval == 0){
return 'This '.$this->format('l');
}
if ($weekInterval == 1){
return 'Next '.$this->format('l');
}
return $this->format('Y-m-d');
}
}
$dates = [
'2013-07-26',
'2013-07-27',
'2013-07-28',
'2013-07-30',
'2013-08-09'
];
foreach($dates as $date){
$date = new SayDate($date);
echo $date->format('Y-m-d') .' -> '. $date->say() . PHP_EOL;
}
// Output:
// 2013-07-26 -> 2013-07-26
// 2013-07-27 -> Today
// 2013-07-28 -> Tomorrow
// 2013-07-30 -> Next Tuesday
// 2013-08-09 -> 2013-08-09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment