PHP:获取一年中的最后一位数(例如:2015 == 5)

时间:2015-10-26 22:36:34

标签: php unix time timestamp

如何将我的功能转换为显示" Y"中的最后一位和/或倒数第二位?标记

这是我的代码:

<?php
$timestamp = time();     //Timestamp from server
$user_time = 1597429549; //Timestamp from database

$time = ($user_time-$timestamp); // Should be around 4 years

echo gmdate("Y m d H i s", $time); //This will output 1974 10 20 19 59 34
?>

如何将其转换为显示时间为:

4 10 20 19 59 34 0004 10 20 19 59 34

而不是:

1974 10 20 19 59 34

2 个答案:

答案 0 :(得分:5)

使用模数运算符:

public function parseADobjectsForTree($aDobjects){
        try
        {
            $result = array();
            $singleObject = array();
            unset($aDobjects["count"]);
            for($i=0; $i < count($aDobjects); $i++)
            {
                if(array_key_exists("name", $aDobjects[$i]))
                {
                    if(in_array("user",$aDobjects[$i]["objectclass"]))
                    {
                        $result[] = array("id"=>$i+1, "text"=>$aDobjects[$i]["name"][0],"subtext"=>$aDobjects[$i]["distinguishedname"][0] );//"li_attr"=>"jstree-leaf"
                    }
                    else
                    {
                        $result[] = array("id"=>$i+1, "text"=>$aDobjects[$i]["name"][0], "subtext"=>$aDobjects[$i]["distinguishedname"][0],"children"=> array());//"li_attr"=>"jstree-closed",
                    }

                }
            }

答案 1 :(得分:0)

Substr 做了这个伎俩,谢谢你们指点我正确的方向。

    <?php
$timestamp = time();     //Timestamp from server
$user_time = 1597429549; //Timestamp from database

$time = ($user_time-$timestamp); // Should be around 4 years

// This did the trick:

$gmdate = gmdate("y m d H i s", $time);
$rest = substr("$gmdate", -16);
?>