今年过了多少天

时间:2012-02-27 09:50:32

标签: php

我想知道从1月1日到今天的当前年份已过去的天数。我正在创建一个唯一的ID,我想将它作为id的一部分。我希望数字固定为三位数,前导零。 Plz的帮助。

e.g。

今天:2012-2-27 然后经过的天数是057.

4 个答案:

答案 0 :(得分:4)

使用{z}参数date函数http://php.net/manual/en/function.date.php,然后printf打印前导零

$day_of_the_year = date( 'z' );
printf( '%03d', $day_of_the_year );

答案 1 :(得分:1)

str_pad(date("z"), 3, "0", STR_PAD_LEFT);

答案 2 :(得分:1)

$startDate = "2012-01-01";
$today = "2012-2-27";

$diff = abs(strtotime($today) - strtotime($startDate ));

$days = floor(($diff/(60*60*24));
if($days < 100) {
   echo "0".$days;
} else {
   echo $days;
}

您也可以通过以下功能获得天数的差异:

function dateDiff ($d1, $d2) {
// Return the number of days between the two dates:

  return round(abs(strtotime($d1)-strtotime($d2))/86400);

}  // end function dateDiff

答案 3 :(得分:0)

$now = new \DateTime('now', new DateTimeZone('Europe/Prague'));
$first_day_of_year = new \DateTime($now->format('Y').'-01-01', new DateTimeZone('Europe/Prague'));

var_dump($first_day_of_year->diff($now)->format('%a'));

今天是2014-10-13,返回285

相关问题