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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:01:38+00:00 2026-06-09T18:01:38+00:00

I have a function that accepts ($year, $month = null, $day = null) Essentially

  • 0

I have a function that accepts ($year, $month = null, $day = null)

Essentially a year always has to be passed in, but the month and day are optional.
If they are not passed in then it sets the range to the biggest possible.

so: call | result

(2012, 08, 15) | ['2012-08-15', '2012-08-15']  
(2012, 08)     | ['2012-08-01', '2012-08-31']
(2012, 02)     | ['2012-02-01', '2012-02-29']
(2012)         | ['2012-01-01', '2012-12-31']
()             | false

I have the below code, however to me it seems needlessly complex, can anyone think of a better version?

if (!is_null($year)) {

  //year
  $from = $year . '-';
  $to   = $year . '-';

  //month
  if (!is_null($month)) {
    $from .= sprintf('%02d', $month) . '-';
    $to   .= sprintf('%02d', $month) . '-';

    //day
    if (!is_null($day)) {
      $from .= sprintf('%02d', $day);
      $to   .= sprintf('%02d', $day);
    } else {
      $from .= '01';
      $to   .= sprintf('%02d', cal_days_in_month(CAL_GREGORIAN, $month, $year));
    }

  } else {
    $from .= '01-31';
    $to   .= '12-31';
  }
  return array($from, $to);
}
return false;
  • 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-09T18:01:39+00:00Added an answer on June 9, 2026 at 6:01 pm

    First of all I would change the design a little so that the usage is more easy:

    function my_func_your_func_date_func($year, $month = null, $day = null)
    {
        if (NULL === $year)
            return false;
    
        $mask = '%04d-%02d-%02d';
        $from = vsprintf($mask, date_pad_first($year, $month, $day));
        $to   = vsprintf($mask, date_pad_last($year, $month, $day));
    
        return array($from, $to);
    }
    

    And then these helper functions:

    function date_pad_first($year, $month = NULL, $day = NULL)
    {
        if (NULL === $month)
            $month = 1;
    
        if (NULL === $day)
            $day = 1;
    
        return array($year, $month, $day);
    }
    
    function date_pad_last($year, $month = NULL, $day = NULL)
    {
        if (NULL === $month)
            $month = 12;
    
        if (NULL === $day)
            $day = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    
        return array($year, $month, $day);
    }
    

    I then might want to extract the differences between these two pretty similar looking functions and parametrice it but I am not sure.


    If you prefer to have it within one function (and with a little different semantics resetting the $day if $month is not set), this needs the branching with an if:

    function my_func_your_func_date_func($year, $month = null, $day = null)
    {
        if (NULL === $year)
            return false;
    
        $from[-1] = $year;
        $to = $from;
    
        if (NULL === $month) {
            $from += [1, 1];
            $to += [12, 31];
        } else {
            $from[2] += [$month, 1];
            $to[2] += [$month, cal_days_in_month(CAL_GREGORIAN, $month, $year)];
        }
    
        $mask = '%04d-%02d-%02d';
        return array(
            vsprintf($mask, $from),
            vsprintf($mask, $to)
        );
    }
    

    This function would also not work if there will be one December in time which has less or more than 31 days.

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

Sidebar

Related Questions

I have a Javascript function that accepts a list of HTML nodes, but it
I have a function that accepts large std::vectors (passed by const & ) and
I have a function that accepts a class (not an instance) and, depending on
I have a function that accepts wildcard keyword parameters: def func(**kargs): doA doB How
I have a function that accepts a generic parameter T that is of type
I have a function that accepts a variable number of parameters: foo (Class... types);
I have a javascript function that accepts a number and performs a mathematical operation
I have a function foo that accepts a Boolean function def foo( f:(_)=>Boolean )
In .NET, the Generics Lists have a sort function that accepts IComparer or Comparison
Does Actionscript have a built-in function that accepts a number and can return a

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.