PHP nu-mi calculeaza varsta bine
Last Updated: Jun 23 2022 09:58, Started by
d3v1l500
, Jun 23 2022 08:09
·
0
#1
Posted 23 June 2022 - 08:09
Va salut! am nevoie de o mica lamurire si un pic de ajutor... nu reusesc sa calculez corect varsta unui om bazandu-ma pe data curenta si data nasterii omului...
Voi lasa mai jos codul. helpers public static function years_passed(string $date_input = '') { // Declare and define two dates $date1 = strtotime($date_input); $date2 = strtotime(date('Y-m-d H:i:s')); // Formulate the Difference between two dates $diff = abs($date2 - $date1); // To get the year divide the resultant date into // total seconds in a year (365*60*60*24) $years = floor($diff / (365 * 60 * 60 * 24)); // To get the month, subtract it with years and // divide the resultant date into // total seconds in a month (30*60*60*24) $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24)); // To get the day, subtract it with years and // months and divide the resultant date into // total seconds in a days (60*60*24) $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24)); // To get the hour, subtract it with years, // months & seconds and divide the resultant // date into total seconds in a hours (60*60) $hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60)); // To get the minutes, subtract it with years, // months, seconds and hours and divide the // resultant date into total seconds i.e. 60 $minutes = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60); // To get the minutes, subtract it with years, // months, seconds, hours and minutes $seconds = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60 - $minutes * 60)); $result = sprintf("%d ani, %d luni, %d zile, %d ore, " . "%d minute, %d secunde", $years, $months, $days, $hours, $minutes, $seconds); return $result; } view code <td><b>Varsta actuala</b></td> <td> @set($birthdate = date('Y-m-d H:i:s', strtotime($data->data_nastere))) {{\App\Helpers\Misc::years_passed($birthdate)}} </td> |
#2
Posted 23 June 2022 - 08:18
In calculul tau toate lunile au 30 de zile si toti anii au 365 de zile. Ti se pare ok?
|
#3
Posted 23 June 2022 - 08:24
LuvRaluK, on 23 iunie 2022 - 08:18, said:
In calculul tau toate lunile au 30 de zile si toti anii au 365 de zile. Ti se pare ok? Poate cineva mai destept ma lumineaza |
#4
Posted 23 June 2022 - 08:38
In primul rand, faptul ca te-ai legat neaparat de secunde e o cerinta, sau asa te-ai gandit tu?
Daca nu, atunci eu zic ca e mai usor sa lucrezi separat cu anul, luna si ziua. In afara de anul de inceput si anul de sfarsit, restul sunt ani intregi, deci ai un punct de pornire. Daca luna de inceput e mai mica sau egala cu luna de sfarsit, atunci mai ai inca un an intreg + cateva luni (te gandesti tu cate), in schimb daca luna de inceput e mai mare atunci mai ai doar X luni (la fel, te gandesti tu cate). Mai departe te descurci |
#6
Posted 23 June 2022 - 09:58
Anunturi
Bun venit pe Forumul Softpedia!
▶ 0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users