Start a new topic

[PHP] Really shitty "Upcoming Birthday Display"

Hi guys (n' gals), 

i copy and pasted a lametric application together, which will show all the upcoming birthdays from a predefined set of people. 



It uses two lametric-frames and displays the two next birthdays coming up. 


If you happen to know three people, which share the same birthdate, the app will show an extra indicator (then it's time to grab your paper calendar).


The code is bad. I know. But it does the job. Just fill in the array and you are all set. 


If you have free time on your hands and like to prettify the code. Please do ;) 

<?php
date_default_timezone_set('Europe/Berlin');

//array with all the birthdays
$peoples = array(
	array('birthday' => '12-01', 'name' => 'Test Aesterson'),
	array('birthday' => '12-01', 'name' => 'Test Besterson'),
	array('birthday' => '14-01', 'name' => 'Test Cesterson'),
	array('birthday' => '14-01', 'name' => 'Test Desterson'),
	array('birthday' => '14-01', 'name' => 'Test Eesterson'),
	array('birthday' => '17-01', 'name' => 'Test Festerson'),
	array('birthday' => '27-01', 'name' => 'Test Gesterson'),
	array('birthday' => '01-02', 'name' => 'Test Hesterson'),
	array('birthday' => '05-02', 'name' => 'Test Iesterson'),
	array('birthday' => '14-02', 'name' => 'Test Jesterson'),
	array('birthday' => '19-02', 'name' => 'Test Kesterson')
);

$sortKey = array_map(function($p) {
  $t = new DateTime();
  $t->setTime(0,0,0);
  $next = DateTime::createFromFormat('d-m-Y', $p['birthday'] . '-' . $t->format('Y'));
  $next->setTime(0,0,0);
  
  if($next < $t) {
    $next = $next->modify('+1 year');
  }
  
  return $next->getTimestamp();
}, $peoples);

//sort the array
array_multisort($sortKey, SORT_ASC, $peoples);

//variable for infotext, when the 3rd person has the same birthday as the second
$undmehr ="";

// set name and date for person 1 and replace dash
$name = $peoples[0]['name'];
$termin = $peoples[0]['birthday'];
$geburtstag = str_replace("-", ".", "$termin").".";

// set name and date for person 2 and replace dash
$name2 = $peoples[1]['name'];
$termin2 = $peoples[1]['birthday'];
$geburtstag2 = str_replace("-", ".", "$termin2").".";


// set variable for infotext
if ($peoples[1]['birthday'] == $peoples[2]['birthday'])
	{
	$undmehr =" und andere.";
	}

// if its today replace date with "today" person 1
if ($peoples[0]['birthday'] == date('d-m'))
	{	
		$geburtstag = "Heute: ";
	}
// if its tomorrow replace date with "tomorrow" person 1
if ($peoples[0]['birthday'] == date("d-m", strtotime("+1 day")))
	{	
		$geburtstag = "Morgen: ";
	}
// if its today replace date with "today" person 2	
if ($peoples[1]['birthday'] == date('d-m'))
	{	
		$geburtstag2 = "Heute: ";
	}
// if its tomorrow replace date with "tomorrow" person 2
if ($peoples[1]['birthday'] == date("d-m", strtotime("+1 day")))
	{	
		$geburtstag2 = "Morgen: ";
	}

	//output two frames
	echo "{ \"frames\": [ { \"index\": 0, \"text\": \"$geburtstag $name\", \"icon\": \"a102\" }, { \"index\": 1, \"text\": \"$geburtstag2 $name2 $undmehr\", \"icon\": \"a102\" } ]}";
 ?>

 


Absolutely, that is not the best possible solution. A self-contained, quick answer was OK with me (for the most part) because my birthday collection has not changed all that significantly over the past couple of years.

As of right now, I have the impression that there will be a significant update for interested developers concerning the manner in which applications are developed (dynamic frames, improved — maybe raw — display-control, preconfigured solutions for ITFTTT and Google), and as a result, my PHP snippet is not intended to be a long-term solution. 

 

 

 

Yes it's definitly not optimal. But since my birthday-collection did not change that much in the last couple of years, a self-contained fast solution was acceptable (for me). 


Right now i have the feeling, that there will be a huge update for interested developers regarding the way applications are coded (dynamic frames, better - maybe raw - display-control, predefined solutions for ITFTTT and Google) - thus my php-snippet is not ment as a long-term-solution. 


It's more of an inbetweener to have yourself remindet until the lametric-team will offer an integrated calendar-fetching-api. 

It's a nice idea, but having the birthdays in code is less than optimal.

Anyone care for an integrated Google Calendar solution? I looked at it briefly, but the Google API for contacts (birthdays don't seem available using the Calendar API?) is a bit of a mess... :/

 

Login or Signup to post a comment