如何获取JSON对象数组中的下个月日期? [英] How to get next month dates in JSON object array?

查看:58
本文介绍了如何获取JSON对象数组中的下个月日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 php代码,如下所示,其中在每个月的第一天,我将第二个JSON对象数组(next_month)内容复制到第一个JSON对象中数组(当前月).

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

php代码:

  $ value = json_decode(file_get_contents('../hyt/dates.json'));如果((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;//Y行$ value-> next_month = array_fill(0,($ next_month_days),nada);//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"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"],下个月":["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"]} 

问题陈述:

我想知道应该在第Z行进行哪些更改,以便在第二个JSON对象数组中能够获取下个月的日期.目前,我正在存储 nada .

在成功执行Y行和Z行之后的11月1日的第一天,我想要在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-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"],下个月":["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"; 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线

解决方案

您正在使用 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; 

I have a php code as shown below in which on the 1st day of every month, I am copying 2nd JSON object array (next_month) content into 1st JSON object array (current_month).

In the 2nd JSON object array (next_month), I want to have next month dates. That will also happen on the 1st day of every month. Currently I am storing nada. Let us suppose that today is 1st day of November.

php code:

$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 
}

The current look of JSON (dates.json) is shown below:

{"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"] }

Problem Statement:

I am wondering what changes I should make at Line Z so that in the second JSON object array, I am able to get next month dates. At present, I am storing nada.

The content which I want in the JSON on the 1st day of November month after successful execution of Line Y and Line Z is:

{"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"] }

This is what I have tried:

This is what I have tried at Line Z but its storing only today's date in JSON object array.

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

解决方案

You are using array_fill, which is used to fill at least part of an array with the same value. I would recommend using a simple for loop:

$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;

这篇关于如何获取JSON对象数组中的下个月日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆