Help - Search - Members - Calendar
Full Version: Calculate Date
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Ronnie
How to Calculate date with php script?
example : my birthday is "may 06 1988" and now "october 02 2007". How many of my age days?

help me plzzz finish my homework. sad.gif
Alex
http://php.net/mktime
http://php.net/time

Use those to calculate your age in seconds, then it's just calculation from there down (hint: find years/months/days in seconds and then do some iterative subtraction). I would provide the function I use for this sort of thing, but I'm not going to give you the answer verbatim when it's for homework.
Ronnie
huh.gif huh.gif huh.gif huh.gif huh.gif
Alex
Specific questions? Where did I lose you? Lots of smilies, as I have already said to you, don't make a post.
Ronnie
I use this script to calculate days, i m not sure about this

CODE
<?php
function CalDate($date1, $date2) {
$stardate = getdate(strtotime($date1));
$enddate = getdate(strtotime($date2));
$kMonth = $stardate[mon];
$kDay = $stardate[mday];
$kYear = $stardate[year];
$d = 0;
$j = $enddate[year] - $stardate[year];
if ($j > 0) {
$c = getdate(strtotime($kYear ."-12-31"));
$d = $c[yday] - $stardate[yday];

for ($i=1;$i<$j;$i++) {
$c = getdate(strtotime($kYear+1 ."-12-31"));
$d = $d + $c[yday] + 1;
}
return $d + $enddate[yday];
}
else return $stardate[yday]-$enddate[yday];
}
print CalDate("2008-01-01","2010-01-02");
?>


Anyone have more simple than this or correct this?
help me.
Ed
If you didn't write that and the you expect to hand it in as 'homework', you should be ashamed of yourself. dry.gif

Alex already gave you the closest to an answer, have you actually read his response?

In layman terms:

- Get current UNIX timestamp with http://www.php.net/time
- Get your birthday timestamp with http://www.php.net/mktime
- Do a subtraction of current - yourbirthday, this is the difference in seconds
- Divide by 86400 (60 * 60 * 24 -- seconds in a day) and floor this value http://www.php.net/floor

That's the difference in days.

http://en.wikipedia.org/wiki/Unix_time

If you did, I apologise.

If you did, then I think you should adopt arithmetic over string matching.
Ronnie
all this stuf make me confuse but thanks guys for help me

i will learn more cool.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.