TheMadCat
TheMadCat

Reputation: 107

PHP day of the year return n-1

A simple snip :

$day_today = date("z");
echo $day_today;

Returns 160 but today is 161.

How to correct this ?

Thanks.

Upvotes: 1

Views: 73

Answers (3)

kaldoran
kaldoran

Reputation: 855

Simply do this :

If you want to set day_today to the correct value :

$day_today = date("z") + 1;
echo $day_today;

else if you just want to print the correct value :

$day_today = date("z");
echo $day_today + 1;

you've got n - 1 cause the date start from 0 (not from 1) set it here :

http://php.net/manual/en/function.date.php

'z' The day of the year (starting from 0) - 0 through 365

Upvotes: 0

YaBCK
YaBCK

Reputation: 3029

Take a look at this page it'll tell you all about date() Link

The reason you're getting 160 and not 161 is because when you use date('z') the date starts from Zero. So it's like like n - 1.

'z' The day of the year (starting from 0) - 0 through 365

Above quote is from the page I link on the first line

So to get what you need, you'll need to do the following

Answer:

$day_today = date("z") + 1;
echo $day_today;

So you're basically making it start from '1' instead of '0'

Upvotes: 0

user4904106
user4904106

Reputation:

We need to added n +1 to adjust for him starting from 1 instead of 0. –

<?php

$day_today = date("z");
echo $day_today+1;

Upvotes: 1

Related Questions