Skip to content

@TomNomNom /say-dates.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
<?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
Something went wrong with that request. Please try again.