Jump to content

SUBIECTE NOI
« 1 / 5 »
RSS
Suprataxa card energie?!

Cum era nivelul de trai cam din a...

probleme cu ochelarii

Impozite pe proprietati de anul v...
 teava rezistenta panou apa calda

Acces in Curte din Drum National

Sub mobila de bucatarie si sub fr...

Rezultat RMN
 Numar circuite IPAT si prindere t...

Pareri brgimportchina.ro - teapa ...

Lucruri inaintea vremurilor lor

Discuții despre TVR Sport HD.
 Cost abonament clinica privata

Tremura toata, dar nu de la ro...

Renault Android

Recomandare bicicleta e-bike 20&#...
 

PHP nu-mi calculeaza varsta bine

- - - - -
  • Please log in to reply
5 replies to this topic

#1
d3v1l500

d3v1l500

    New Member

  • Grup: Junior Members
  • Posts: 23
  • Înscris: 26.04.2017
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
LuvRaluK

LuvRaluK

    Active Member

  • Grup: Members
  • Posts: 1,569
  • Înscris: 27.09.2006
In calculul tau toate lunile au 30 de zile si toti anii au 365 de zile. Ti se pare ok?

#3
d3v1l500

d3v1l500

    New Member

  • Grup: Junior Members
  • Posts: 23
  • Înscris: 26.04.2017

View PostLuvRaluK, 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?
Da, asa e... nu e ok.. dar nu am nici macar o idee cum sa implementez ceva sa tina cont de fiecare an in parte de cate zile a avut sau de fiecare an in parte cand feb a fost 28 sau 29 ...
Poate cineva mai destept ma lumineaza

#4
LuvRaluK

LuvRaluK

    Active Member

  • Grup: Members
  • Posts: 1,569
  • Înscris: 27.09.2006
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 ;)

#5
MarianG

MarianG

    be that as it may

  • Grup: Moderators
  • Posts: 31,383
  • Înscris: 10.08.2005
https://yourblogcoac...f-birth-in-php/

#6
d3v1l500

d3v1l500

    New Member

  • Grup: Junior Members
  • Posts: 23
  • Înscris: 26.04.2017

View PostMarianG, on 23 iunie 2022 - 08:53, said:

Mersi frumos! asta m-a ajutat, doar ca am renuntat la afisarea lunilor si zilelor

Anunturi

Bun venit pe Forumul Softpedia!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Forumul Softpedia foloseste "cookies" pentru a imbunatati experienta utilizatorilor Accept
Pentru detalii si optiuni legate de cookies si datele personale, consultati Politica de utilizare cookies si Politica de confidentialitate