Page 1 of 1
Adding days to given $date
Posted: Tue Jun 22, 2010 5:37 am
by simonmlewis
Code: Select all
$today = (date('Y-m-d'));
$todaydate = strtotime($today);
$tolddate = strtotime($row->winnerinformeddate);
$finaldate = strtotime('+30 days', $tolddate);
if ($finaldate <= $todaydate)
{ echo "do this";}
else { echo "do that"; }
Date is entered into the db field.
The user has 30 days to update it.
So $finaldate is meant to work out an additional 30 days onto the $row->winnerinformeddate.
Thing is, $finaldate is coming out LOWER that $todaydate, even though the $row->winnerinformeddate in the field is actually lower (ie. 18 May. So easily outside the 30 days.
How have I coded this wrong?
If I render the variables, these are what come out, and I don't understand the maths.
[text]$todaydate = 2010-06-22 / 1276815600
$finaldate = 2010-06-18 / 1277161200[/text]
How can $todaydate actually have a lower figure than $finaldate, when $todaydate is greater?
Re: Adding days to given $date
Posted: Tue Jun 22, 2010 6:18 am
by Weirdan
simonmlewis wrote:How have I coded this wrong?
strtotime() expects unix timestamp as it's second parameter, and you're giving it string representation of a date.
Edit: You could also use DateTime, which supports comparison internally:
Code: Select all
$now = new DateTime;
$final = new DateTime($row->winnerinformeddate);
$final->modify('+30 days');
if ($final <= $now) {
echo 'do this';
} else {
echo 'do that';
}
Re: Adding days to given $date
Posted: Tue Jun 22, 2010 6:46 am
by simonmlewis
Brilliant.
I never knew about new DateTime, or that modify method.
Works perfectly.
Thanks.
Re: Adding days to given $date
Posted: Fri Jul 23, 2010 5:23 am
by simonmlewis
Code: Select all
$now = new DateTime;
$result = mysql_query ("SELECT * FROM cart");
while ($row = mysql_fetch_object($result))
{
$final = new DateTime($row->entrydate);
$final->modify('+5days');
if ($final > $now)
{
mysql_query ("DELETE from CART WHERE id = '$row->id'");
}
} mysql_free_result($result);
Any reason why this won't delete the row? The date in the row is 14th July. Date today is 23rd July. So it should delete that one row.
PROBLEM SOLVED - IT WAS THE 'CART' CASING.