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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:31:18+00:00 2026-06-10T03:31:18+00:00

I am using the below method to calculate when the next time in a

  • 0

I am using the below method to calculate when the next time in a schedule of times is.

public static function getNextUtcTimestampFromSchedule($schedule, $timezone)
{
    $timestamps = array();
    foreach ($schedule as $day => $times){
        $dt = new \DateTime('next '.self::getDayString($day), new \DateTimeZone($timezone));
        foreach ($times as $time){
            list($hour, $min) = explode(':', $time);
            $dt->setTime($hour, $min);
            $timestamps[] = $dt->getTimestamp();
        }
    }
    sort($timestamps);
    return $timestamps[0];
}

$schedule is an array, like this:

$schedule = array(
    0 => array('11:00', '17:00'), 
    1 => array('10:00', '18:00'),
    2 => array('09:00', '18:00'), 
    3 => array('11:00', '17:00'),
    4 => array('11:00', '16:00'),
    5 => array('15:00', '16:00'),
    6 => array('11:00', '12:00'),
);

getDayString simply converts, for example, 0 to ‘sunday’.

In order to write unit tests, I need to spoof the time that now() is at, or pass it into the method and use it.

The question is, how can I use that with DateTime?

i.e. DateTime('next wednesday') needs to be told what time ‘now’ is, in order to figure out ‘next wednesday’ in a predictable way, so that it can be unit tested.

  • 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-10T03:31:20+00:00Added an answer on June 10, 2026 at 3:31 am

    The __construct function has an optional param:

    public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )
    

    An example:

    <?php
    try {
        $date = new DateTime('2000-01-01');
    } catch (Exception $e) {
        echo $e->getMessage();
        exit(1);
    }
    
    echo $date->format('Y-m-d');
    ?>
    

    Edit: You can use strtotime to generate a timestamp, then use date to generate the string to pass to the new object:

    $date = new DateTime(date("Y-m-d", strtotime("next wednesday")));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the method below to get an array from my plist and
I am getting wrong result using below method. public double evaluate(final double leftOperand, final
I'm using the below method to reply to mails coming in to a business
As I created a Progress bar using below code in a on click method
I have problem with android 4.0.3. I'm using the method below to get local
I am using the code below, its basically babylonian method, how can i modify
I'm having a problem de-compressing a ZIP file using Java. The method is below.
Below is a Matlab function that implements the triangulation method as described in Multiple
Am using below third party API in my project development http://undesigned.org.za/2007/10/22/amazon-s3-php-class I have done
I am using below code to upload excel and INSERT in mysql in php

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.