改变从美国格式到英国的数组元素

时间:2012-12-16 13:02:51

标签: php arrays

我从外部API中提取电影数据,为我最后一年的大学项目创建类似于IMDb的数据库,发布日期信息的格式为 YYYY-MM-DD (即16 2012年12月显示为2012-12-16,而不是16-12-2012)。有没有办法重新排序阵列的各个部分,以便为英国英语观众重新调整它们?

发布日期的原始结果如下所示,从TMDb API中的getMovieReleases函数收集(NB保存在帖子大小上,我只复制了getMovieReleases函数的前10个结果): / p>

[id] => 17578
    [countries] => Array
        (
            [0] => Array
                (
                    [iso_3166_1] => US
                    [certification] => PG
                    [release_date] => 2011-12-21
                )

            [1] => Array
                (
                    [iso_3166_1] => DE
                    [certification] => 6
                    [release_date] => 2011-10-27
                )

            [2] => Array
                (
                    [iso_3166_1] => FR
                    [certification] => 
                    [release_date] => 2011-10-26
                )

            [3] => Array
                (
                    [iso_3166_1] => TR
                    [certification] => 
                    [release_date] => 2011-11-04
                )

            [4] => Array
                (
                    [iso_3166_1] => DK
                    [certification] => 7
                    [release_date] => 2011-10-27
                )

            [5] => Array
                (
                    [iso_3166_1] => GB
                    [certification] => PG
                    [release_date] => 2011-10-26
                )

            [6] => Array
                (
                    [iso_3166_1] => FI
                    [certification] => 12
                    [release_date] => 2011-11-04
                )

            [7] => Array
                (
                    [iso_3166_1] => IT
                    [certification] => T
                    [release_date] => 2011-10-28
                )

            [8] => Array
                (
                    [iso_3166_1] => SE
                    [certification] => 
                    [release_date] => 2011-10-28
                )

            [9] => Array
                (
                    [iso_3166_1] => NL
                    [certification] => 6
                    [release_date] => 2011-10-26
                )

            [10] => Array
                (
                    [iso_3166_1] => BE
                    [certification] => 
                    [release_date] => 2011-10-26
                )
        )

)

我在PHP&中调用数据HTML如下,仅挑选美国和英国的发布日期:

$releases = $tmdb->getMovieReleases($tmdb_id);

$uk = $releases['countries']['14']['release_date'];
$us = $releases['countries']['0']['release_date'];

<section>
    <p class="title">RELEASE DATE</p>
    <p class="section_body">
        <?php echo $uk; ?> (UK)<br>
        <?php echo $us; ?> (US)
    </p>
</section>

2 个答案:

答案 0 :(得分:2)

您想格式化这样的日期吗?

<?php
        $source = '2011-11-04';
    $date = new DateTime($source);
    echo $date->format('d.m.Y')."<br/>"; // 04.11.2012
    echo $date->format('d-m-Y')."<br/>"; // 04-11-2011
?>

答案 1 :(得分:2)

$uk = date('d/m/Y', strtotime($releases['countries']['14']['release_date']));

这应该指向正确的方向..

请参阅date()strtotime()