Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:36:16+00:00 2026-06-01T20:36:16+00:00

I am trying to check if a MySQL stored datetime object is within 2

  • 0

I am trying to check if a MySQL stored datetime object is within 2 hours of right now and just either show certain links or not based on if the ride_hour more than 2 hours in the future of the current hour. Here’s what i’ve 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’m comparing the day first to see if it is even happing today, if so i don’t need to check the hour. otherwise, I’m trying to compare $ride_hour with $now_hour. The day is obviously working but the hour comparison does not seem to be working.

The other thing I am trying to do, is in the cakePHP controller, is limit the results based on a sort value. I am trying to limit the results to either, ALL, Results with a date in the future, or results in the past, and paginate. Here’s how I’m trying which isn’t working:

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 you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:36:17+00:00Added an answer on June 1, 2026 at 8:36 pm

    This would be much easier with the DateTime functionality in PHP. I’m not exactly sure what you mean by “within 2 hours”. I’m assuming you mean between 2 hours before and 2 hours after. If not you can change the intervals pretty easily.

    $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.
    }
    

    Documentation: http://php.net/DateTime

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use MySQL 5.5.16. I'm trying to check if a certain column exists in
I'm trying to understand MySQL Stored Procedures, I want to check if a users
I'm just trying to create my first mysql stored procedure and I'm trying to
Trying to check if a name is already stored in the database from the
I am using MySQL v5.1.36 and I am trying to create a stored function
I am trying to write simple mysql stored procedure and it seems that I
I am trying to write a stored procedure in MySQL which will perform a
I am trying to do a simple stored procedure in mysql which has a
I am trying to check for people in a certain age range. I have
I've been trying to insert check boxes next to values retrieved from a MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.