Forum FAQForum FAQSearchSearch MemberlistMemberlist Forum ignore listForum ignore list RegisterRegister ProfileProfile Log in to check your private messagesLog in to check your private messages Log inLog in
PHP skripta koja ispisuje koliko si star?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Server-side
View previous topic :: View next topic  
Author Message
Limeni



Joined: 13 Nov 2007
Posts: 109

PostPosted: 14.04.2009 02:03    Post subject: PHP skripta koja ispisuje koliko si star? Add user to your forum ignore list Reply with quote

Evo napravio sam neku skriptu, ali posto sa PHP-om jos nisam najbolji imam osjecaj da se ovo moze napravit jednostavnije i sa manje koda. Laughing

Uglavnom skripta treba izracunati i ispisati koliko mi je godina, poznat joj je datum mog rodenja.

Ovako sam napravio nakon 20ak minuta kopanja po manual-u:

Code:
$now = date("Y-m-d");
$my_birth = "1985-03-06";

$now_ar = explode("-", $now);         
$my_birth_ar = explode ("-", $my_birth);
      
$year = $now_ar[0] - $my_birth_ar[0];
$month = $now_ar[1] - $my_birth_ar[1];
$day = $now_ar[2] - $my_birth_ar[2];
         
$age_in_days = ($year*360) + ($month*30) + $day;
$age_in_years = $age_in_days / 365;
         
echo floor($age_in_years);


Jel postoji nacin da se ovo jednostavnije napise i jeli ovo 100% tocno u dan?
Back to top
View user's profile Send private message
AntonioZg



Joined: 02 Feb 2009
Posts: 2
Location: Zagreb

PostPosted: 14.04.2009 10:26    Post subject: Add user to your forum ignore list Reply with quote

evo ja sam ovako u 4 reda koda:

Code:
$now = time();
$my_birth = strtotime ("7.9.1986");
$age = floor (($now - $my_birth) / 31536000);
echo  "Imam $age god.";


varijabla $now sadrzi broj sekundi od 1970 god do sada, $my_birth sadrzi broj sekundi od 1970 do datuma mog(tvog) rođenja.. u varijabli $age to dvoje oduzmes i podijelis sa brojem sekundi u jednoj godini.. evo to je onako najjednostavije..

pozz

_________________
Croativno.com
Back to top
View user's profile Send private message Visit poster's website
ghosap



Joined: 18 Sep 2003
Posts: 79

PostPosted: 14.04.2009 10:28    Post subject: Add user to your forum ignore list Reply with quote

Code:

$my_birth = '1985-03-06';
$age_seconds = time() - strtotime($my_birth);


i sad radis sa tim sekundama dalje kaj trebas ...
Dani :
Code:
$aga_days = $age_seconds / 86400;

_________________
. . . and when the balance is lost all that is left is the reckening . . .
Back to top
View user's profile Send private message
Limeni



Joined: 13 Nov 2007
Posts: 109

PostPosted: 14.04.2009 12:30    Post subject: Add user to your forum ignore list Reply with quote

Odlicno, znao sam da se moze to i krace napravit Very Happy

Ma naletio sam odmah na strtotime al sam intuitivno pomislio da ona pretvara string u neki od vremenskih formata, al ustvari racuna koliko je sekundi proslo od 1970 do datuma kojeg mu stavis, tak da imas dva datuma i racunas sto ti treba, odlicno.

Hvala...
Back to top
View user's profile Send private message
zytzagoo
mi3.crew


Joined: 25 Aug 2003
Posts: 1842
Location: Zagreb, Hrvatska

PostPosted: 14.04.2009 12:52    Post subject: Add user to your forum ignore list Reply with quote

Izbjegavajte strtotime(), naletit cete na mine: prestupne godine, godine prije 1970/1900 (ovisno o verziji php-a i os-a)

Run this, pa se igrajte:
Code:

<?php

function get_age($year, $month, $day) {
    $ydiff = date('Y') - (int) $year;
    // check if this year's birthday isn't reached
    if (date('m') < (int) $month || (date('m') == (int) $month && date('d') < (int) $day)) {
        $ydiff--;
    }
    echo __FUNCTION__ . '(' . $year . ', ' . $month . ', ' . $day . '): ' . $ydiff . "\n";
    return $ydiff;
}

/**
* testing, testing, 1, 2, 3
*/
$bdays = array(
    '1.1.2006',
    // leap year
    '1.1.2000',
    '1.1.1970',
    // works for dates before unix epoch too!
    '1.1.1940',
    '1.1.1902',
    '1.1.1901',
    // future dates return negative values
    '1.1.2010',
    '1.1.2039',
    // works on dates before 1900 as well!
    '1.1.1900',
    // leap year before 1900!
    '2.3.1896',
    // leading zeros shouldn't matter
    '15.09.1980',
    '15.9.1980'
);

foreach ($bdays as $bday) {
    $b = explode('.', $bday);
    get_age($b[2], $b[1], $b[0]);
}


_________________
[+]I[+]am[+]my[+]own[+]religion[+]
Back to top
View user's profile Send private message Visit poster's website Twitter profile
Limeni



Joined: 13 Nov 2007
Posts: 109

PostPosted: 14.04.2009 17:30    Post subject: Add user to your forum ignore list Reply with quote

Evo ga taman se poveselim kak stvari mogu bit jednostavne i onda mi ovaj zakomplicira zivot Smile

Salim se zytzagoo, ovo super radi Smile
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    mi3dot.org Forum Index -> Server-side All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group