Reputation: 14628
I want to get number of days elapsed from the current years Jan 1st to today date. I am making a unique id and I want to use it as part of the id. I want number to fixed to three digits with leading zeros. Plz help.
e.g.
Today : 2012-2-27 Then number of days elapsed is 057.
Upvotes: 1
Views: 1489
Reputation: 13
$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'));
today is 2014-10-13, returns 285
Upvotes: 0
Reputation: 15593
$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;
}
You can get the difference of days the by following function also:
function dateDiff ($d1, $d2) {
// Return the number of days between the two dates:
return round(abs(strtotime($d1)-strtotime($d2))/86400);
} // end function dateDiff
Upvotes: 1
Reputation: 13755
Use the 'z' argument to the date
function, http://php.net/manual/en/function.date.php, then printf
to print the leading zeroes
$day_of_the_year = date( 'z' );
printf( '%03d', $day_of_the_year );
Upvotes: 4