Jump to content

[SOLVED] How Do You Subtract Dates?


JayJ

Recommended Posts

Hi all,

I want to be able to disable a user account after a month.

How do I subtract today's date from the stored date?  Here's my basic code...

$timein = $row['timein'];
$timenow = date('M j Y');

$difference = $timein - $timenow;

If($difference > 30)
{ DISABLE ACCOUNT }

But how do I take the difference between dates if they're in a 'M j Y' format.  Does PHP know how to make the difference?

Thanks.

Jay
Link to comment
https://forums.phpfreaks.com/topic/35807-solved-how-do-you-subtract-dates/
Share on other sites

The easiest way is to use mktime() and make a timestamp from your readable date.  If you don't track the time just use noon (12pm).

then subtract that from time();

this will yield the distance in seconds, between the two dates.

[code]

//plugin your own vars where I have the month, day, year ...

$distance = mktime(12, 00, 00, date('m'), date('d'), date('Y')) - time();

[\code][/code]
Thanks guys.  So far, I only know very basic php.  So your help is appreciated.

I do have a couple more questions though.

Right now, I insert the registration time into my database like this...

[code]
$regtime = date('M,j,Y');  // I record it as a VARCHAR
[/code]

Can I leave it like this and use the mktime() function, or do I need to change both the way I record it and the variable type, or just one?

If I don't have to change any of that, do I continue like this...

[code]
$regtime = mktime(0, 0, 0, date("m")-1, date("d"),  date("Y"));
$currentstatus = $regtime - $expiretime;

If($currentstatus < 0)
{ DISABLE USER; }
else
{ ALLOW USER; }
[/code]
Hey guys, been fiddling with this code, but I still can't seem to get it to do what I want.  A little help?

Again, I want users to have a trial period of, say, 20 days.  When 20 days expires, their user account is disabled.

Any suggestions?

[code]
<?php
$result = mysql_query("SELECT * FROM loginphp")
or die(mysql_error());

$num=mysql_numrows($result);

$i=0;
while ($i < $num)
{
$row = mysql_fetch_array( $result );
$time = $row['time'];  // Time of registration
$trial = mktime(00, 00, 00, date('m'), date('d')-20, date('Y'));

$difference = $trial - $time;

If($difference < 20)
{
echo "<font face=verdana size=2>DISABLE USER: " . $row['Uname'] . "</font><br>";
}
else
{
echo "<font face=verdana size=2>ALLOW USER: " . $row['Uname'] . "</font><br>";
}
$i++;
}
?>
[/code]
It does it automatically.
"mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input. For example, each of the following lines produces the string "Jan-01-1998"."

http://us2.php.net/mktime
Read the examples Shogun and Jay.
Well, when I take the difference from a timestamp, I get something like this...

-2.0068932674E+13

No wonder I was getting back inaccurate stuff.  How can I take the difference when the results aren't even coming back as a number?

Some suggestions on this would be very helpful.
I am working on someting similar: I want out-of-date events to be deleted. so when an end date has passed it autoamtically deleted the event, however I also unfortunalty have my dates stored as varchars and in this format - 01/01/2007.
Is it possiblwe to subtract dates using this data?
ldoozer, I'm not sure using a VARCHAR would work.  I tried that with another function in my script, but I ended up having to change it to DATETIME.  Maybe that's a better route for us.

If we could simply extract the Month and Day individually from the date field.  Then coding some simple math equations would be rather easy, I think.

Do you know how to pull, say, a date('d') into a single $variable?  I've tried, but got no effect yet.  Something like...

[code]
<?php
$datereg = $row['datereg']; // Get date from database

$dayreg = date('d', $datereg); // Pull out the day value out of the stored date.
$dayexp = date('d')-20; // substract 20 days off of today to get the 20 day expiry.

$checkuser = $dayreg - $dayexp // get the difference

If($checkuser > 0) // if the value is positive, it should be expired
{ EXPIRED; }
else
{ ALLOWED; }
?>
[/code]

Of course, we'd have to take into consideration when the month (and year) change.  Some more simple equations.  But, certainly, there's got to be an easier way!  I'm a noob to this stuff.

What do you think?
I think Im gonna have to cut my losses and change the field type to a date field then use something like the below, which deletes rows that are 30 days old:


$query = ("DELETE FROM your_table WHERE (TO_DAYS(NOW()) - TO_DAYS(date)) > 30") or die ("Error in Update sql: ". mysql_error());
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
If you store the epoch timestamp as an INT field, most time operations are very easy, and very flexible.

You would store the timestamp using the PHP [b]time()[/b] function.

Then, to delete old ones, simply:
[code]
$time_to_delete = time() - (60*60*24*30); //Now ,minus 30 days
$query = "DELETE FROM your_table WHERE date<$time_to_delete";
[/code]

Also, you can easily compare and subtract timestamps in INT format.
Hey guys, I got some help somewhere else.  Here's what's working for me...

[code]
<?php

$result = mysql_query("SELECT *,Uname, date_format(time, '%e') as fmt, date_format(time, '%b') as fmt2, date_format(time, '%Y') as fmt3 FROM loginphp order by Uname asc") 
or die(mysql_error());

while($row = mysql_fetch_array( $result ))
{
$user = stripslashes($row['Uname']); // username
$name = stripslashes($row['Fname']); // first name
$email = stripslashes($row['Email']); // email address
$status = stripslashes($row['status']); // membership status: trial, monthly, yearly
$regtime = stripslashes($row['fmt']);  // day of registration
$month = stripslashes($row['fmt2']); //month of registration
$year = stripslashes($row['fmt3']); //year of registration
$day = date("d");
$day2 = $day - 21; // 7 day trial expiry email notice
$day3 = $day - 28; // trial period
$trial_mon = date("M"); //current month
$trial_year = date("Y"); //current year

if($status == 0) // Tries only those on a trial membership, which is STATUS = 0
{

if($trial_mon == $month && $trial_year == $year)
{

if($regtime == $day2)
{
echo "$user: SEND NOTICE<br>"; // Sends a 7 day notice for expiry
}
else
{
if($regtime <= $day3)
{
echo "$user: DISABLE USER<br>"; // Deletes user account
}
else
{
echo "$user: ALLOW USER<br>";
}
}
}
else
{

if($trial_year - $year == 1 && $trial_mon == $month)
{
$days = 31-$regtime;
$days = $days+$day;
if($days == $day2)
{
echo "$user: SEND NOTICE<br>";
}
else
{
echo "$user: ALLOW USER<br>";
}
}
else
{
if($days <= $day3)
{
echo "$user: DISABLE USER<br>";
}
else
{
echo "$user: SEND NOTICE<br>";
}
}

}
}
}
?>
[/code]

Enjoy!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.