Days Since

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

I am using PHP to put the date something was submitted into my DB.
I am doing complex calculations on another page that are determined by the number of days that have passed since that day.
For example:
Someone enters their username/password on the 4th of September
On the 25th of september, they have been with me for 21 days. I charge them for 21 days based on other info.
It is simple enough to do it if they are in that month, but if they signed up on August 29th, it won't simply deduct it by using php's date(j) function with J as the argument

Thanks

Laughing out loud

Busy's picture

He has: 6,151 posts

Joined: May 2001

how about doing it by the hour (or min) then converting that back to days

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

What do you mean??? How could it count it?
I am running cron programs with this, I could set it up to add 1 day to a table column 'daysince' but that would take a lot of modifying, and I'd prefer to keep it as I have it, if possible

Laughing out loud

Busy's picture

He has: 6,151 posts

Joined: May 2001

here's a script out of "PHP and MYSQL development" book I found

<?php

//set date for calculation
$day = 18;
$month = 9;
$year = 1972;

//remember you need bday as day month year
$bdayunix = mktime (\"\",\"\",\"\",$month,$day,$year); //get unix ts for bday
$nowunix = time(); //get unix ts for today
$ageunix = $nowunix - $bdayunix; //work out the difference
$age = floor($ageunix / (365 * 24 * 60 * 60)); //convert from seconds to years

echo \"Age is
$age\";
?>

this is for a birthday script to find out how old the person is. Could be easily changed for your needs, I believe
further info on calendar dates at
date and time
http://php.net/manual/ref.datetime.php
http://mysql.com/documentation/mysql/commented/manual.php?section=Date_and_time_functions

calendar functions
http://php.net/manual/ref.calendar.php

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

mktime() is my latest probability... I had just seen that on php.net when I got this reply...
I'll try it out, but if anyone else has better suggestions, I'd love to hear them
Thanks

Laughing out loud

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Oh ya know what... that works perfectly with a bit of tweeking
(Just in case someone wants a working version)

<?php
$query
= \"SELECT * FROM dates WHERE (date) LIKE ('1') \";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo mysql_error();
$day = $row[day];
$month = $row[month];
$year = $row[year];

//remember you need bday as day month year
$bdayunix = mktime (\"\",\"\",\"\",$month,$day,$year); //get unix ts for bday
$nowunix = time(); //get unix ts for today
$ageunix = $nowunix - $bdayunix; //work out the difference
$age = floor($ageunix / (24 * 60 * 60)); //convert from seconds to years

echo \"Age is
$age\";

?>

Of course, it'd be different values for $row[] but you get the point... that shows up as 242 days... the number of days from January 1st 2002
Thanks a billion busy

Laughing out loud

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Even easier to through the whole thing at the SQL server...

<?php
// assuming one 'date' field
$query = \"SELECT (TO_DAYS(SYSDATE()) - TO_DAYS(date)) as age FROM dates\";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo mysql_error();
$age = $row[age];
echo \"Age is
$age\";


// not tested
// assuming 'day', 'month', 'year' fields
$query = \"SELECT (TO_DAYS(SYSDATE()) - TO_DAYS(CONCAT(year,'-',month,'-',day))) as age FROM dates\";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
echo mysql_error();
$age = $row[age];
echo \"Age is
$age\";
?>

Mark Hensler
If there is no answer on Google, then there is no question.

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

date is 1 field... in the form of date("m\/j\/Y")
I could make it 3 fields... but that would totally complicate things

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.