Page 1 of 1

Finding out the day of the year it is. (Not what you think.)

Posted: Tue Sep 26, 2006 5:11 pm
by Jza
I'm not talking about using date(); to find out what day of the year it is. I want to find out the day of the year it is.

For example: January 18th, is the 18th day of the year, Febuary 1st is the 31st day of the year, you know what I mean? I'm well aware that date(); provides a function to do this. But I want to do something such as 01 (january) 18(day), and the program to say, this is the 18th day of the year, not find the current day, understand?

please help.

Posted: Tue Sep 26, 2006 5:17 pm
by volka
e.g. mktime or strtotime can create a timestamp for the given date.

Posted: Tue Sep 26, 2006 5:31 pm
by Weirdan
strtotime + date
understand?
Can't rightly say I do.

Posted: Tue Sep 26, 2006 5:43 pm
by Jza
hahah, sorry, it was a dumb question.

echo date("z", mktime(0, 0, 0, 12, 31, 2006));

this produces 364, december 31th is the 365th day of the year, is it not?

Posted: Tue Sep 26, 2006 5:46 pm
by Benjamin

Code: Select all

echo "This is the " . date("zS") . " day of the year.";

Posted: Tue Sep 26, 2006 5:48 pm
by Jza
no, I mean it's a day off.

Posted: Tue Sep 26, 2006 5:56 pm
by volka
Jza wrote:this produces 364, december 31th is the 365th day of the year, is it not?
http://de2.php.net/date wrote:z The day of the year (starting from 0)
You can increment the value if you want to, e.g.

Code: Select all

<?php 
$d = getdate( mktime(12, 0, 0, 12, 31, 2006) );
echo $d['yday'] + 1;
?>

Posted: Tue Sep 26, 2006 5:57 pm
by Weirdan
this produces 364, december 31th is the 365th day of the year, is it not?
That depends on what number did you start to count. E.g. was the January 1st the first day in the year, or was it the day number zero?
PHP manual wrote: z The day of the year (starting from 0)

Posted: Tue Sep 26, 2006 5:58 pm
by Jza
ya volka, I was thinking of doing that, I jsut wanted to see if it could be avoided.

Posted: Tue Sep 26, 2006 5:59 pm
by Benjamin

Code: Select all

preg_match('#([0-9]{1,3})([a-z]{0,2})#i', date("zS"), $x);
echo "This is the " . ($x['1'] + 1) . $x['2'] . " day of the year.";

Posted: Tue Sep 26, 2006 5:59 pm
by DrTom
It starts counting at 0. Check out
http://www.php.net/manual/en/function.date.php for more specifics on how it works.[/url]

Posted: Tue Sep 26, 2006 6:03 pm
by Jza
ya, starting at 1 would be better, it's all good though, thanks alot.

Posted: Wed Sep 27, 2006 4:46 am
by Mordred

Code: Select all

preg_match('#([0-9]{1,3})([a-z]{0,2})#i', date("zS"), $x);
echo "This is the " . ($x['1'] + 1) . $x['2'] . " day of the year.";
It's just not correct.
If the returned day is 1, you would see: "This is the 2th day of the year.";
Also the 'S' format character works for the day of the month.

The correct way:
date('z')+1. Check the last digit (after +1) and choose the correct ordinal suffix (st, nd, rd, th)

Posted: Wed Sep 27, 2006 5:24 am
by Benjamin
Yeah I realized that after I posted it. At least it gives the correct day of the year though :)

Posted: Wed Sep 27, 2006 6:41 am
by Rovas
Have you look at some the example scripts given at the bottom of the page. It think the last one will give some clue how to do it (that what i understand from your post).