操纵在PHP中的日期数组 [英] Manipulating arrays of dates in PHP

查看:124
本文介绍了操纵在PHP中的日期数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在 PHP 很多接近一块code的难度。我有日期和值的数组,例如:

 日期=(2014-12-01,2014-12-02,2014-12-08,2014-12-09,2014-12-10,2014-12-11 )
值=(5,3,7,8,9,2)

您会注意到 12/01 是星期一,由于是 4/09 。我想从形成这两个数组的数组4:

 周一=(5,7)
周二=(3,8)
周三=(0,9)
周四=(0,2)

您会注意到,该阵列由抓住为期一周的日子相关的值形成的。然而,在一个星期三日期存在,例如但现有星期二没有,那么该阵列应当具有一个0的情况。换言之,在4阵列都应该是相同的长度。

谁能帮我写在PHP code来实现这一目标?在此先感谢!

注意:到目前为止,我只确定如何从一个日期查找星期:日期(L,的strtotime(2014-12- 08)); 我真的想不通一般的算法来解决这个


解决方案

  $日期=阵列('2014年12月1日,2014年12月2日,2014- 12-08,2014年12月9日,
                 2014年12月10日,2014年12月11日');
$值=阵列(5,3,7,8,9,2);$日期=的strtotime(分($日期));
$停止=的strtotime(MAX($日期));
$日期= array_flip($日期);
$ OUT =阵列();而($日期和LT = $停止)
{
   $ TMP =日期('Y-M-D',$日期);
   $出[日期(L,$日期)] [] =使用isset($日期[$ TMP])及和放大器;使用isset($值[$日期[$ TMP])?
                              $值[$日期[$ TMP]:0;
   $日期=的strtotime('+1天',$日期);
}的print_r($出);

结果:

 阵列

    [周一] =>排列
        (
            [0] =>五
            [1] => 7
        )    [周二] =>排列
        (
            [0] => 3
            [1] => 8
        )    [周三] =>排列
        (
            [0] => 0
            [1] => 9
        )    [周四] =>排列
        (
            [0] => 0
            [1] => 2
        )    [周五] =>排列
        (
            [0] => 0
        )    [六] =>排列
        (
            [0] => 0
        )    [日] =>排列
        (
            [0] => 0
        ))

PS:<?code>我怎么能得到包括与之关联的日期阵列中的所有日期的数组只有在所有星期一

修改code,例如:

  $ TMP =日期('Y-M-D',$日期);
   $存在=使用isset($日期[$ TMP])及和放大器;使用isset($值[$日期[$ TMP]);
   $出[日期(L,$日期)] ['数字'] [] = $的存在? $值[$日期[$ TMP]:0;
   如果($存在)$出来[日期(L,$日期)] ['日期'] [] = $ tmp目录;
   $日期=的strtotime('+1天',$日期);

您会得到一个输出(例如对于星期一)

  [周一] =&GT;排列
    (
        [数字] =&GT;排列
            (
                [0] =&GT;五
                [1] =&GT; 7
            )        [日期] =&GT;排列
            (
                [0] =&GT; 2014年12月1日
                [1] =&GT; 2014年12月8日
            )    )

I'm having a lot of difficulty approaching a piece of code in PHP. I have an array of dates and values, for example

dates = (2014-12-01,2014-12-02,2014-12-08,2014-12-09,2014-12-10,2014-12-11)
values = (5,3,7,8,9,2)

You'll note that 12/01 is a Monday, as is 12/08. I'd like to form 4 arrays from these two arrays:

monday = (5,7)
tuesday = (3,8)
wednesday = (0,9)
thursday = (0,2)

You'll note that the arrays are formed by grabbing the values associated with the days of the week. However, in the case that a Wednesday date exists, for example, but the prior Tuesday does not, then the array should have a "0". In other words, the 4 arrays should all be the same length.

Can anyone help me write code in PHP to achieve this? Thanks in advance!

NOTE: So far, I have only determined how to find the day of the week from a date: date('l', strtotime("2014-12-08")); I really can't figure out the general algorithm to solve this.

解决方案

$dates  = array( '2014-12-01','2014-12-02','2014-12-08','2014-12-09',
                 '2014-12-10','2014-12-11' );
$values = array( 5, 3, 7, 8, 9, 2 );

$date  = strtotime(min($dates));
$stop  = strtotime(max($dates));
$dates = array_flip($dates);
$out   = array();

while($date <= $stop)
{
   $tmp = date('Y-m-d', $date);
   $out[date('l', $date)][] = isset($dates[$tmp]) && isset($values[$dates[$tmp]]) ?
                              $values[$dates[$tmp]] : 0;  
   $date = strtotime('+1 day', $date);   
}

print_r($out);

Result:

Array
(
    [Monday] => Array
        (
            [0] => 5
            [1] => 7
        )

    [Tuesday] => Array
        (
            [0] => 3
            [1] => 8
        )

    [Wednesday] => Array
        (
            [0] => 0
            [1] => 9
        )

    [Thursday] => Array
        (
            [0] => 0
            [1] => 2
        )

    [Friday] => Array
        (
            [0] => 0
        )

    [Saturday] => Array
        (
            [0] => 0
        )

    [Sunday] => Array
        (
            [0] => 0
        )

)

ps: how can I get the an array of all the dates included in the "dates" array associated with only all the Mondays?

Modify the code as, for example:

   $tmp = date('Y-m-d', $date);
   $exists = isset($dates[$tmp]) && isset($values[$dates[$tmp]]);
   $out[date('l', $date)]['numbers'][] = $exists ? $values[$dates[$tmp]] : 0; 
   if ($exists) $out[date('l', $date)]['dates'][] = $tmp;
   $date = strtotime('+1 day', $date);  

You'll get an output as (example for monday)

[Monday] => Array
    (
        [numbers] => Array
            (
                [0] => 5
                [1] => 7
            )

        [dates] => Array
            (
                [0] => 2014-12-01
                [1] => 2014-12-08
            )

    )

这篇关于操纵在PHP中的日期数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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