Adding days to given $date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Adding days to given $date

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Adding days to given $date

Post 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';
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Adding days to given $date

Post by simonmlewis »

Brilliant.
I never knew about new DateTime, or that modify method.
Works perfectly.

Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Adding days to given $date

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply