tags:

views:

27

answers:

2

Basically what I want to do is to send an email to follow up on order placed in 3 hours later.

What I try to do is to get the current date (up to hours level), then compare with the orderdate+ 3 hours, to see whether it match.

Example orderdate: 2010-06-12 18, after add 3 hours should become 2010-06-12 21

However, after add hour, the final result sometimes become bigger, sometimes become smaller... something wrong.

$now= date('Y-m-d H');
echo $now . "<br>";


...
...

while ($data = mysql_fetch_array ($result))
{
 $orderid = $data['orderid'];
 $orddate = $data['orddate'];
 $corddate= date('Y-m-d H:i:s', $orddate);


 $year = substr ($corddate, 0, 4);
 $month = substr ($corddate, 5, 2);
 $day = substr ($corddate, 8, 2);
 $hour = substr ($corddate, 11, 2);
 $actualorderdate= $year . "-" . $month . "-" . $day . " " . $hour;


 $new_time = mktime ($hour+3, 0, 0, $month, $day, $year);
 $year = date ('Y', $new_time);
 $month = date ('m', $new_time);
 $day=  date ('d', $new_time);
 $hour=  date ('h', $new_time);
 $xhourslater= $year . "-" . $month . "-" . $day . " " . $hour;


 echo "actualorderdate: ". $actualorderdate. " xhourslater: " . $xhourslater. "<br>";

 if($now==$xhourslater)
 {
  echo "now is 3 hours later" . "<br>";
  //send email follow up, then update status as sent, in order not to resend anytime in the 3rd hour.

 }


}
+1  A: 

Seems to me like you're doing a lot of excess parsing for nothing.

$orddate = $data['orddate'];
$actualOrderDate = date('Y-m-d H:i:s', $orddate);
$timeToSendEmail = date('Y-m-d H:i:s', strtotime('+3 hours', $orddate)); //Assuming $orddate is a timestamp int. If not, you'll have to do some calculations or something there, possibly using date() again.

Basically, you just want to have the order time + 3 hours, so use php's strtotime (http://php.net/manual/en/function.strtotime.php) function. Very helpful.

If that's not what you want, please post some test data so it's easier to figure out what might be going funky.

Jeff Rupert
A: 

I believe it's simpler to

$date_now  = date( "Y-m-d g:i a" ); // get the date
$time_now  = time( $date_now ); // convert it to time
$time_next = $time_now + 3 * 60 * 60; // add 3 hours (3 x 60mins x 60sec)
$date_next = date( "Y-m-d H", $time_next); // convert it back to date
Anax