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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:39:46+00:00 2026-05-16T04:39:46+00:00

I dont understand DatePeriod, DateInterval classes very well. This question is linked to another

  • 0

I dont understand DatePeriod, DateInterval classes very well. This question is linked to another one – How to display converted time zones in a 'generic week' (Sunday thru Saturday)? wherein I want to parameterize the solution offered by artefacto.

Help would be appreciated!

This is artefacto’s code:

$tz1 = new DateTimezone("Asia/Calcutta");
$indiaAvail = array(
    new DatePeriod(new DateTime("2010-08-01 10:00:00", $tz1),
        new DateInterval("PT2H15M"), 1)
);

This is what I came up with:

function shift_timezones_onweek($from_timezone, $from_timebegin, $from_timeend, $to_timezone)
{

    $tz1 = new DateTimezone($from_timezone);

    $datetime1 = new DateTime("2010-08-01 $from_timebegin", $tz1);
    $datetime2 = new DateTime("2010-08-01 $from_timeend", $tz1);

    $interval = $datetime1->diff($datetime2);

    $indiaAvail = array(
        new DatePeriod($datetime1, $interval, 1)
    );
    ...

As artefacto points out, “there’s no point in building a DatePeriod from two times just to have it decomposed immediately after into those two dates”, however I dont understand how I can modify this easily to make it work with the rest of his code (which requires the $indiaAvail to exist as it is I guess…)

  • 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-16T04:39:46+00:00Added an answer on May 16, 2026 at 4:39 am

    The DatePeriod class stores a start date, an end date and an interval (or, equivalently, a start date, an interval and a number of repetitions). It is Traversable, and when iterated in a foreach loop, it will yield all the dates (DateTime objects) from the start to the end, with the given interval separating them.

    Therefore, iterating a DatePeriod class is the same as iterating an array composed of all the dates that would be yielded by DatePeriod.

    So we can write:

    //...
    $tz1 = new DateTimezone($from_timezone);
    
    $datetime1 = new DateTime("2010-08-01 $from_timebegin", $tz1);
    $datetime2 = new DateTime("2010-08-01 $from_timeend", $tz1);
    
    $indiaAvail = array(
        array($datetime1, $datetime2),
    );
    
    
    $tz2 = new DateTimezone($to_timezone);
    //convert periods:
    $times = array_map(
        function (array $p) use ($tz2) {
           $res = array();
           foreach ($p as $d) {
               $res[] = $d->setTimezone($tz2);
           }
           return $res;
        },
        $indiaAvail
    );
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a little site for car renting,but i dont understand very well javascript.
I dont understand javascript syntax well,my question: How jquery define click function for get
I dont understand this why i get this error! is so simple yet very
I have the structure below that I dont understand the output very well. var
according to this code i dont understand about NSClassFromString how did i know viewControllerName
If i run this code the application will crash, and i dont understand why,
Dont understand why I am getting stack level too deep when I run this
I dont understand what the layout is, in the view. I asked a question
I dont understand what is wrong. If I run this command in command prompt
I have search for this problem for over 2 hours and i dont understand

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.