PHP / CakePHP time and time comparison

I am trying to check if a MySQL datetime data object is stored for 2 hours and just show specific links or not based on whether the trip will be more than 2 hours in the future of the current hour. Here is what I tried:

$now = date('Y-m-d');
$now_hour = date('h');
$ride_date = date('Y-m-d',strtotime($ride['Ride']['date']));
$full_ride_hour =  date('h',strtotime($ride['Ride']['date']));
$ride_hour = $full_ride_hour-2;

So, I compare the first day to see if this happens even today, if so I do not need to check the hour. otherwise, I'm trying to compare $ ride_hour with $ now_hour. The day obviously works, but the clock comparison doesn't seem to work.

The other thing I'm trying to do is in the cakePHP controller, restricts the results based on the sort value. I try to limit the results to either: ALL, results with a date in the future, or results in the past, or paginate. Here, as I try, that does not work:

switch ($sort) {
case 0:
$rides = $this->paginate('Ride',array('Ride.user_id' => $user_id,'Ride.date > now()'));
break;

case 1:
$rides = $this->paginate('Ride',array('Ride.user_id' => $user_id));
break;

case 2:
$rides = $this->paginate('Ride',array('Ride.user_id' => $user_id,'Ride.date < now()'));
break;
}

$this->set('sort',$sort);
$this->set('rides', $rides);

Thank!

+3
1

DateTime PHP. , " 2 ". , 2 2 . , .

$mysql_date_obj = new DateTime($date_from_mysql);
$before_time = new DateTime('now');
$before_time->add( DateInterval::createFromDateString('-2 hour') );
$after_time = new DateTime('now');
$after_time->add( DateInterval::createFromDateString('2 hour') );

if( $before_time <= $mysql_date_obj && $mysql_date_obj <= $after_time ) {
     // date is within 2 hours.
}

: http://php.net/DateTime

+4

All Articles