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 7615421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:38:27+00:00 2026-05-31T02:38:27+00:00

I have a cronjob, that kicks in every 5 minutes. It should do some

  • 0

I have a cronjob, that kicks in every 5 minutes. It should do some tasks only at specific times of the day (e.g. morning and evening).

What’s php’s most effective / elegant way to determine if the DateTime of now is in between the 5 minute time frame in that the cronjob may kick in?

At the moment I’m doing:

$date = new DateTime();

$hour = (int)$date->format('H');
$min = (int)$date->format('i');

if($hour == 7 && ($min >= 40 || $min < 45)) {
    // Do something in the morning
}

if($hour == 21 && ($min >= 00 && $min < 05)) {
    // Do something in the evening
}

But this seems like a lot of code. Ain’t there something like

$date->isInTimeRane($begin, $end);

as native php code?

  • 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-05-31T02:38:29+00:00Added an answer on May 31, 2026 at 2:38 am

    You can extend DateTime to add your own methods to it. I would do it this way:-

    class MyDateTime extends DateTime
    {
        /**
        * Checks if this DateTime is between two others
        * @param DateTime $start
        * @param DateTime $end
        * @return boolean 
        */
        public function inRange(DateTime $start, DateTime $end){
            return ($this >= $start && $this <= $end);
        }
    }
    

    Then you can simply do:-

    $begin = new DateTime($sometime);
    $end = new DateTime($someLaterTime);
    $myTime = new MyDateTime($yetAnotherTime);
    var_dump($myTime->inRange($begin, $end);
    

    That is the cleanest way I can think of of doing it and pretty much what you asked for.

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

Sidebar

Related Questions

I have 1 cronjob that runs every 60 minutes but for some reason, recently,
If I have a cron job that run every 10 minutes and for some
i have a php script that should be run automatically every day. as the
I have a cronjob that runs every hours and parse 150,000+ records. Each record
Lets say I have a cronjob like this: every 1.day, :at => '4:30 am'
I have an application that reads an xml file every 5 minutes (using a
I have a cronjob that calls a PHP file via CURL every hour from
I have a cronjob calling a shell script. Inside that shell script and I
I have a PHP page that I run every minute through a CRON job.
Some setup background first: I have a cronjob which runs a PHP file called

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.