php - How can I write neat code to get the last birthday date? -



php - How can I write neat code to get the last birthday date? -

i know day of birth, lets say: 1996-12-12 know today is: 2014-11-12

now want date of lastly birthday. , have written solution, don't complexity of code. how can write more readable other programmers?

$lstrbirthday = '1996-12-12'; $lstrlastbirtday = (strtotime(date('y').date('-m-d', strtotime($lstrbirthday))) > strtotime('now')) ? (date('y') - 1).date('-m-d', strtotime($lstrbirthday)) : date('y').date('-m-d', strtotime($lstrbirthday));

thanks!

its easier if utilize datetime functions

$begin = new datetime( '1996-12-12' ); $end = new datetime(); $end = $end->modify( '-1 day' ); $interval = new dateinterval('p1y'); $daterange = new dateperiod($begin, $interval ,$end); $bd = ''; foreach($daterange $date){ $bd = $date->format("ymd"); }

edit: have lastly entry ;)

something can't test can foreach on period. if date in past should lastly birthday. if create +1 year in future.

edit: @pascalvgemert perhaps bit more readable you...

public function getlastbirthdate($startdate) { $begin = new datetime( $startdate ); $end = new datetime(); $end = $end->modify( '-1 day' ); $interval = new dateinterval('p1y'); $daterange = new dateperiod($begin, $interval ,$end); $bd = ''; foreach($daterange $date){ $bd = $date->format("ymd"); } homecoming $bd; } $lastbd = getlastbirthdate('1996-12-12');

php date strtotime date-arithmetic

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -