public
Created

  • Download Gist
say-dates.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<?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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.