代码之家  ›  专栏  ›  技术社区  ›  flash

如何在JSON对象数组中获取下个月的日期?

  •  0
  • flash  · 技术社区  · 4 年前

    我有一个 php code 如下图所示,在每个月的第一天,我复制第二个JSON对象数组 (next_month) (current_month) .

    在第二个JSON对象数组中 (下个月) ,我想订下个月的日期。这也将发生在每个月的第一天。目前我正在储存 nada . 让我们假设今天 1st day of November .

    php代码:

    $value = json_decode(file_get_contents('../hyt/dates.json'));
        
    if ((date('j') == 1)) {
        $month = 11;
        $year = date('Y');
        $current_month_days = (date('t', strtotime($year . '-' . $month . '-01')));
        $next_month_days = (date('t', strtotime($year . '-' . ($month + 1) . '-01')));
        $value->current_month = $value->next_month;   // Line Y
        $value->next_month = array_fill(0, ($next_month_days), nada);    // Line Z 
    }
    

    JSON的当前外观 (dates.json)

    {"current_month": ["2020-10-01", "2020-10-02", "2020-10-03", "2020-10-04", "2020-10-05", "2020-10-06", "2020-10-07", "2020-10-08", "2020-10-09", "2020-10-10", "2020-10-10", "2020-10-12", "2020-10-13", "2020-10-14", "2020-10-15", "2020-10-16", "2020-10-17", "2020-10-18", "2020-10-19", "2020-10-20", "2020-10-21", "2020-10-22", "2020-10-23", "2020-10-24", "2020-10-25", "2020-10-26", "2020-10-27", "2020-10-28", "2020-10-29", "2020-10-30","2020-10-31"], 
    "next_month": ["2020-11-01", "2020-11-02", "2020-11-03", "2020-11-04", "2020-11-05", "2020-11-06", "2020-11-07", "2020-11-08", "2020-11-09", "2020-11-11", "2020-11-11", "2020-11-12", "2020-11-13", "2020-11-14", "2020-11-15", "2020-11-16", "2020-11-17", "2020-11-18", "2020-11-19", "2020-11-20", "2020-11-21", "2020-11-22", "2020-11-23", "2020-11-24", "2020-11-25", "2020-11-26", "2020-11-27", "2020-11-28", "2020-11-29", "2020-11-30"] }
    

    问题陈述:

    我想知道我应该做些什么改变 所以在第二个JSON对象数组中,我可以得到下个月的日期。目前,我正在储存 娜达

    {"current_month": ["2020-11-01", "2020-11-02", "2020-11-03", "2020-11-04", "2020-11-05", "2020-11-06", "2020-11-07", "2020-11-08", "2020-11-09", "2020-11-11", "2020-11-11", "2020-11-12", "2020-11-13", "2020-11-14", "2020-11-15", "2020-11-16", "2020-11-17", "2020-11-18", "2020-11-19", "2020-11-20", "2020-11-21", "2020-11-22", "2020-11-23", "2020-11-24", "2020-11-25", "2020-11-26", "2020-11-27", "2020-11-28", "2020-11-29", "2020-11-30"], 
    "next_month": ["2020-12-01", "2020-12-02", "2020-12-03", "2020-12-04", "2020-12-05", "2020-12-06", "2020-12-07", "2020-12-08", "2020-12-09", "2020-12-11", "2020-12-11", "2020-12-12", "2020-12-13", "2020-12-14", "2020-12-15", "2020-12-16", "2020-12-17", "2020-12-18", "2020-12-19", "2020-12-20", "2020-12-21", "2020-12-22", "2020-12-23", "2020-12-24", "2020-12-25", "2020-12-26", "2020-12-27", "2020-12-28", "2020-12-29", "2020-12-30", "2020-12-31"] }
    

    这就是我尝试过的:

    这是我在Z行尝试过的,但它只在JSON对象数组中存储今天的日期。

    $value->next_month = array_fill(0, ($next_month_days), date("Y-m-d")); //Z线

    0 回复  |  直到 4 年前
        1
  •  0
  •   jspit    4 年前

    我认为你应该完全重新创建你的JSON字符串。从当月的第一天开始。循环始终运行,直到月份结束。接下来的一个月,整个事情又发生了。

    $arr = $cur = [];
    $date = date_create('first day of this month 00:00');
    $startMonth = $month = $date->format('m');
    while($startMonth == $month){
      $cur[] = $date->format('Y-m-d');
      $date->modify('+1 Day');
      $month = $date->format('m');
    }
    $arr["current_month"] = $cur;
    $startMonth = $month;
    $cur = [];
    while($startMonth == $month){
      $cur[] = $date->format('Y-m-d');
      $date->modify('+1 Day');
      $month = $date->format('m');
    }
    $arr["next_month"] = $cur;
    $jsonStr = json_encode($arr);
    
        2
  •  0
  •   Technoh    4 年前

    array_fill ,它至少用于填充数组的一部分 . 我建议使用一个简单的for循环:

    $next_month_array = [];
    $next_month = $month < 12 ? $month + 1 : 1;
    $year = date('Y');
    
    for($day_counter = 1; $day_counter <= $next_month_days; $day_counter++) {
      $next_month_array[] = "$year-$next_month-$day_counter";
    }
    
    $value->next_month = $next_month_array;