Created
January 7, 2015 02:21
-
-
Save ricardosiri68/ce8e85d25057dbb4f91c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
date_default_timezone_set("America/Argentina/Buenos_Aires"); | |
/** | |
*---------------------------------------------------------------------------- | |
* Birthday | |
* | |
* reprecenta el la fecha proxima de cumpleaños de una persona basandose en su | |
* fecha de nacimiento | |
* | |
* @category datetime | |
* @package own | |
*---------------------------------------------------------------------------- | |
*/ | |
class Birthday | |
{ | |
private $__day; | |
private $__month; | |
private $__year; | |
public function __construct($day, $month, $year) | |
{ | |
/* fecha actual */ | |
$this->__today = time(); | |
$this->__today_date = getdate($this->__today); | |
/* valida la fecha */ | |
if (! checkdate($month, $day, $year)) { | |
throw new Exception("La fecha es invalida: $day/$month/$year"); | |
} else { | |
$this->__day = $day; | |
$this->__month = $month; | |
$this->__year = $year; | |
} | |
/* dia ordinal del año de la fecha actual */ | |
$today_yday = $this->__today_date['yday']; | |
/* dia ordinal del año de la fecha de nacimiento */ | |
$birth_yday = getdate(mktime(0, 0, 0, $month, $day, $year)); | |
$birth_yday = $birth_yday['yday']; | |
/* sello de tiempo del cumpleaños que se aproxima */ | |
$this->__birth_timestamp = ($birth_yday >= $today_yday) ? | |
mktime(0, 0, 0, $month, $day, $this->__today_date['year']): | |
mktime(0, 0, 0, $month, $day, $this->__today_date['year'] + 1); | |
/* array con atributos de la fecha que se aproxima */ | |
$this->__birthdate_array = getdate($this->__birth_timestamp); | |
} | |
/** | |
* getter que recupera el sello de tiempo timestamp de la fecha en la que | |
* se aproxima el compleaños | |
* | |
* @return {int} sello de tiempo | |
*/ | |
public function timestamp() | |
{ | |
return $this -> __birth_timestamp; | |
} | |
/** | |
* obtiene la fecha segun el formato deseado | |
* | |
* @param {string} $format formato en el que se quiere reprecentar la fecha | |
* ej dd/m/Y | |
* | |
* @return {string} salida de la fecha en el formato correspondiente | |
*/ | |
public function format($format) | |
{ | |
return date($format, $this->__birth_timestamp); | |
} | |
/** | |
* array asociativo con los diferentes atributos de la fecha | |
* para mas informacion ver: http://php.net/manual/es/function.getdate.php | |
* | |
* @return {integer} array de la fecha | |
*/ | |
public function date_array() | |
{ | |
return $this->__birthdate_array; | |
} | |
/** | |
* una slida del mes y el año ej: January of 1992 | |
* | |
* @return {string} salida de formato del mes y el año | |
*/ | |
public function year_month() | |
{ | |
$date = $this->date_array(); | |
return $date['month'] . ' of ' . $date['year']; | |
} | |
/** | |
* devuelve una salida de la semana del año y el año. Tener en cuenta que | |
* la fecha que agrupa esta clave hace correspondencia al año al que | |
* pertenece la semana y no al año de la fecha ej: | |
* 53 of 1993 | |
* 01/01/1994 | |
* | |
* @return {string} salida del formato semana del año | |
*/ | |
public function year_week() | |
{ | |
$week = (int)date("W", $this->__birth_timestamp); | |
if (52 >= $week) { | |
return date("W \of Y", $this->__birth_timestamp); | |
} else { | |
$year = (int)date("Y", $this->__birth_timestamp); | |
$year--; | |
return "$week of $year"; | |
} | |
} | |
} | |
/** | |
*---------------------------------------------------------------------------- | |
* BrithDays | |
* | |
* colecciona, ordena, agrupa y filtra los cumpleaños segun la fecha actual y | |
* demas parametros | |
* | |
* @category collection | |
* @package own | |
*---------------------------------------------------------------------------- | |
*/ | |
class BirthDays | |
{ | |
public function __construct($birthdays) | |
{ | |
/* fecha actual */ | |
$this->__today = time(); | |
$this->__today_date = getdate($this->__today); | |
/* cumpleaños */ | |
$this->__birthdays = $birthdays; | |
} | |
/** | |
* ordena los cumpleaños de manera decendente | |
* | |
* @return {array} array ordenado de forma decendente | |
*/ | |
public function sorted() | |
{ | |
$birthdays = $this->__birthdays; | |
usort( | |
$birthdays, | |
function ($x, $y) { | |
if ($x->timestamp() == $y->timestamp()) { | |
return 0; | |
} else if ($x->timestamp() < $y->timestamp()) { | |
return -1; | |
} else { | |
return 1; | |
} | |
} | |
); | |
return $birthdays; | |
} | |
/** | |
* agrupa las fechas segun un methodo del objeto Birthday | |
* | |
* @param {string} $method_date clave del nombre del metodo de Birthday | |
* | |
* @return {array} array de grupos | |
*/ | |
private function group_by($method_date) | |
{ | |
$groups = []; | |
foreach ($this -> sorted() as $birthday) { | |
$key_date = $birthday->$method_date(); | |
if (isset($groups[$key_date])) { | |
array_push($groups[$key_date], $birthday); | |
} else { | |
$groups[$key_date] = [$birthday,]; | |
} | |
} | |
return $groups; | |
} | |
/** | |
* filtra los cumpleaños segun un sello de tiempo determinado | |
* | |
* @param {integer} $date_to_match sello de tiempo (timestamp) | |
* | |
* @return {array} cumpleaños filtrados | |
*/ | |
private function __date_match($date_to_match) | |
{ | |
$birthdays = []; | |
$date_to_match = date("Y-m-d", $date_to_match); | |
foreach ($this->__birthdays as $birthday) { | |
$date = date("Y-m-d", $birthday->timestamp()); | |
if ($date == $date_to_match) { | |
array_push($birthdays, $birthday); | |
} | |
} | |
return $birthdays; | |
} | |
/** | |
* filtra los cumpleaños segun un atributo selector de la funcion date para | |
* mas informacion ver la lista de selctores de formato de date: | |
* http://php.net/manual/es/function.date.php | |
* | |
* @param {string} $attr atributo selector de al fecha | |
* @param {string} $match_exp valor al que debe coincidir | |
* | |
* @return {array} coincidiencias | |
*/ | |
private function __attr_match($attr, $match_exp) | |
{ | |
$matches = []; | |
foreach($this->__birthdays as $birthday){ | |
$birth_attr = date($attr, $birthday->timestamp()); | |
if ($birth_attr == $match_exp) { | |
array_push($matches, $birthday); | |
} | |
} | |
return $matches; | |
} | |
/** | |
* agrupa los cumpleaños segun el mes en el que se festejan | |
* | |
* @return {array} cumpleaños agrupados | |
*/ | |
public function group_by_months() | |
{ | |
return $this->group_by('year_month'); | |
} | |
/** | |
* agrupa los cumpleaños segun la semana del año en que se festejan | |
* | |
* @return {array} cumplaños agrupados | |
*/ | |
public function group_by_weeks() | |
{ | |
return $this->group_by('year_week'); | |
} | |
/** | |
* filtra todos los cumpleaños que se celebran hoy | |
* | |
* @return {array} cumplaños filtrados | |
*/ | |
public function today() | |
{ | |
return $this->__date_match($this->__today); | |
} | |
/** | |
* filtra todos los cumpleaños que se celebran el dia de mañana | |
* | |
* @return {array} cumplaños filtrados | |
*/ | |
public function tomorrow() | |
{ | |
return $this->__date_match($this->__today + (24 * 60 * 60)); | |
} | |
/** | |
* filtra todos los cumpleaños que se celebran esta semana | |
* | |
* @return {array} cumplaños filtrados | |
*/ | |
public function this_week() | |
{ | |
$today_week = date("YW", $this->__today); | |
return $this->__attr_match("YW", $today_week); | |
} | |
/** | |
* filtra todos los cumpleaños que se celebran la semana que viene | |
* | |
* @return {array} cumpleaños filtrados | |
*/ | |
public function next_week() | |
{ | |
$next_week = (string)(1 + (int)date("YW", $this->__today)); | |
return $this->__attr_match("YW", $next_week); | |
} | |
/** | |
* filtra todos los cumpleaños que se celebran este mes | |
* | |
* @return {array} cumpleaños filtrados | |
*/ | |
public function this_month() | |
{ | |
$today_month = date("Ym", $this->__today); | |
return $this->__attr_match("Ym", $today_month); | |
} | |
/** | |
* filtra todos los cumpleaños que se celebran el mes que viene | |
* | |
* @return {array} cumpleaños filtrados | |
*/ | |
public function next_month() | |
{ | |
$next_month = (string)(1 + (int)date("Ym", $this->__today)); | |
return $this->__attr_match("Ym", $next_month); | |
} | |
} | |
?> |
This file contains hidden or 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 | |
require_once 'birthday.php'; | |
$birthdays = new Birthdays( | |
[ | |
new Birthday(1, 1, 1988), | |
new Birthday(24, 1, 1988), | |
new Birthday(5, 1, 1988), | |
new Birthday(6, 1, 1985), // fecha actual | |
new Birthday(7, 1, 1985), // mañana | |
new Birthday(13, 1, 1944), // la semana que viene | |
new Birthday(7, 2, 1990), | |
new Birthday(28, 2, 1985), | |
new Birthday(16, 3, 1993), | |
new Birthday(30, 3, 1984), | |
new Birthday(9, 4, 1991), | |
new Birthday(15, 5, 1985), | |
new Birthday(12, 5, 1980), | |
new Birthday(26, 6, 1997), | |
new Birthday(6, 7, 1975), | |
new Birthday(27, 8, 1993), | |
new Birthday(10, 9, 1974), | |
new Birthday(23, 9, 1972), | |
new Birthday(16, 10, 2000), | |
new Birthday(18, 11, 1973), | |
new Birthday(14, 11, 1993), | |
new Birthday(19, 12, 1994) | |
] | |
); | |
?> | |
<h1>Agrupadas por mes</h1> | |
<?php foreach ($birthdays->group_by_months() as $month => $month_birthdays): ?> | |
<dl class="month"> | |
<dt><?=$month?><dt> | |
<dd> | |
<?php foreach ($month_birthdays as $birthday): ?> | |
<p><?=$birthday->format("d/m/Y")?></p> | |
<?php endforeach; ?> | |
</dd> | |
</dl> | |
<?php endforeach; ?> | |
<h1>Agrupadas por semana del año</h1> | |
<?php foreach($birthdays->group_by_weeks() as $week => $week_birthdays): ?> | |
<dl class="week"> | |
<dt><?=$week?></dt> | |
<dd> | |
<?php foreach ($week_birthdays as $birthday): ?> | |
<p><?=$birthday->format("d/m/Y")?></p> | |
<?php endforeach; ?> | |
</dd> | |
</dl> | |
<?php endforeach; ?> | |
<h1>Que se celebran hoy</h1> | |
<div class="today"> | |
<?php foreach($birthdays->today() as $birthday): ?> | |
<p><?=$birthday->format("d/m/Y")?></p> | |
<?php endforeach; ?> | |
</div> | |
<h1>Que se celebran mañana</h1> | |
<div class="tomorrow"> | |
<?php foreach($birthdays->tomorrow() as $birthday): ?> | |
<p><?=$birthday->format("d/m/Y")?></p> | |
<?php endforeach; ?> | |
</div> | |
<h1>Que se celebran esta semana</h1> | |
<div class="this-week"> | |
<?php foreach($birthdays->this_week() as $birthday): ?> | |
<p><?=$birthday->format("d/m/Y")?></p> | |
<?php endforeach; ?> | |
</div> | |
<h1>Que se celebran la semana que viene</h1> | |
<div class="next-week"> | |
<?php foreach($birthdays->next_week() as $birthday): ?> | |
<p><?=$birthday->format("d/m/Y")?></p> | |
<?php endforeach; ?> | |
</div> | |
<h1>Que se celebran este mes</h1> | |
<div class="this-month"> | |
<?php foreach($birthdays->this_month() as $birthday): ?> | |
<p><?=$birthday->format("d/m/Y")?></p> | |
<?php endforeach; ?> | |
</div> | |
<h1>Que se celebran el mes que viene</h1> | |
<div class="next-month"> | |
<?php foreach($birthdays->next_month() as $birthday): ?> | |
<p><?=$birthday->format("d/m/Y")?></p> | |
<?php endforeach; ?> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment