Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created May 5, 2020 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/335dd1f053128acc2928f6928961c1f8 to your computer and use it in GitHub Desktop.
Save parzibyte/335dd1f053128acc2928f6928961c1f8 to your computer and use it in GitHub Desktop.
<?php
// parzibyte.me/blog
function obtener_edad_segun_fecha($fecha_nacimiento)
{
$nacimiento = new DateTime($fecha_nacimiento);
$ahora = new DateTime(date("Y-m-d"));
$diferencia = $ahora->diff($nacimiento);
return $diferencia->format("%y");
}
// Probar
$fechas = ['2020-05-05', '1990-01-01', '1999-01-01', '2002-12-31'];
foreach($fechas as $fecha){
printf("Edad para %s: %d\n", $fecha, obtener_edad_segun_fecha($fecha));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment