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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jza
Forum Newbie
Posts: 17
Joined: Sat Sep 23, 2006 10:10 am

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

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

e.g. mktime or strtotime can create a timestamp for the given date.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

strtotime + date
understand?
Can't rightly say I do.
Jza
Forum Newbie
Posts: 17
Joined: Sat Sep 23, 2006 10:10 am

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

echo "This is the " . date("zS") . " day of the year.";
Jza
Forum Newbie
Posts: 17
Joined: Sat Sep 23, 2006 10:10 am

Post by Jza »

no, I mean it's a day off.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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)
Jza
Forum Newbie
Posts: 17
Joined: Sat Sep 23, 2006 10:10 am

Post by Jza »

ya volka, I was thinking of doing that, I jsut wanted to see if it could be avoided.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.";
Last edited by Benjamin on Tue Sep 26, 2006 5:59 pm, edited 1 time in total.
DrTom
Forum Commoner
Posts: 60
Joined: Wed Aug 02, 2006 8:40 am
Location: Las Vegas

Post 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]
Jza
Forum Newbie
Posts: 17
Joined: Sat Sep 23, 2006 10:10 am

Post by Jza »

ya, starting at 1 would be better, it's all good though, thanks alot.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yeah I realized that after I posted it. At least it gives the correct day of the year though :)
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post 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).
Post Reply