/    Sign up×
Community /Pin to ProfileBookmark

In need of the day of the year

Hi I have a date:

[code=php]
$date = “2006-12-03”;
[/code]

now I need to get the day of the year for $date is there a way i can do this in php?

Please help me.

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@pcthugJan 26.2007 — Like:
[code=php]
$date = "2006-12-03";
echo date('z', strtotime($date)); // 336
[/code]
Copy linkTweet thisAlerts:
@gogelpotauthorJan 26.2007 — Thanks

Was not to bright of me not to convert the $Date string to date format.
Copy linkTweet thisAlerts:
@coppocksJan 26.2007 — Just explode the variable string:
[code=php]
$dateArray = explode('-',$date);
$year = $dateArray[0];
$month = $dateArray[1];
$day = $dateArray[2];
[/code]

All this does, is separate the string by the '-' and places the numerics into an array or list. I think this is what you want.
Copy linkTweet thisAlerts:
@bokehJan 26.2007 — [B]Note:[/B] date('z') starts counting from zero, hence 2007-01-01 will return [B]0[/B] and 2007-01-02 will return [B]1[/B].
Copy linkTweet thisAlerts:
@NightShift58Jan 26.2007 — [code=php]
$date = "2006-12-03";
echo date('z', strtotime($date)); // 336
[/code]
[/QUOTE]
This works because we have a valid date. The moment something isn't quite right, for example "2006-02-35", strtotime() has its problems... The safest solution, I think, would be:[code=php]<?php
$date = "2006-02-35";
echo date("z", mktime(0,0,0,substr($date,5,2),substr($date,8,2),substr($date,0,4));
// optionally, to make it "real life",as implied by Bokeh:
echo date("z", mktime(0,0,0,substr($date,5,2),substr($date,8,2),substr($date,0,4))+1;
?>[/code]
Copy linkTweet thisAlerts:
@bokehJan 26.2007 — [code=php]
echo date("z", mktime(0,0,0,substr($date,5,2),substr($date,8,2),substr($date,0,4))+1;[/code]
[/QUOTE]

[CODE]echo date("z", mktime(0,0,0,substr($date,5,2),substr($date,8,2),substr($date,0,4))[B][color=red])[/color][/B]+1;[/CODE]
Copy linkTweet thisAlerts:
@NightShift58Jan 26.2007 — picky, picky... ?

I had so many in there already, I figured ...
Copy linkTweet thisAlerts:
@pcthugJan 27.2007 — This works because we have a valid date. The moment something isn't quite right, for example "2006-02-35", strtotime() has its problems... The safest solution, I think, would be:[code=php]<?php
$date = "2006-02-35";
echo date("z", mktime(0,0,0,substr($date,5,2),substr($date,8,2),substr($date,0,4));
// optionally, to make it "real life",as implied by Bokeh:
echo date("z", mktime(0,0,0,substr($date,5,2),substr($date,8,2),substr($date,0,4))+1;
?>[/code]
[/quote]

Your solution is still suseptical to vulnerabilities, try the following. It initially checks the format and then checks it's validity via the checktime() function:
[code=php]
if (preg_match('/(d{4})-(d{2})-(d{2})/', $date, $t) && checkdate($t[2], $t[3], $t[1]))
{
echo date('z', strtotime($date))+1; // 337
}
else
{
die('Invalid date argument.');
}[/code]
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — [code=php]
if (preg_match('/(d{4})-(d{2})-(d{2})/', $date, $t) && checkdate($t[2], $t[3], $t[1])) {
echo date('z', strtotime($date))+1; // 337
} else {
die('Invalid date argument.');
}[/code]
[/QUOTE]
Excellent... Now the only improvement left would be:[code=php]
if (preg_match('/(d{4})-(d{2})-(d{2})/', $date, $t) && checkdate($t[2], $t[3], $t[1])) {
echo date('z', mktime(0, 0, 0, $t[2], $t[3], $t[1]))+1; // 337
} else {
die('Invalid date argument.');
}[/code]
It makes PHP's life just a little easier. In the end, internally, that's what's going to happen anyway.
Copy linkTweet thisAlerts:
@pcthugJan 27.2007 — ... internally, that's what's going to happen anyway.[/quote]Well actually, the passed string is parsed by Zend into a timestamp, mktime is never referenced in the source code of the strtotime function declaration.
Copy linkTweet thisAlerts:
@pcthugJan 27.2007 — With that said, the strtotime function consumes ~30% more memory and ~15% more execution time than the mktime function when both are passed [I]simple[/I] arguments.
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — mktime is never referenced in the source code...[/quote]I agree. I didn't think it would be since [B]mktime()[/B] is not a source-level function. But internally, the code behind [B]mktime()[/B] is certain to be lower-level than [B]strtotime()[/B], if nothing but to cover the various possibilities that it must deal with.

But my point was not whether [B]strotime()[/B] is able to do what it was meant to do - there's no question about that. My point was and remains that [B]mktime()[/B] has a built-in adjustment/correction mechanism that provides a safety net, where needed.

Other than that, I'm not married to any particular date function. They all have their use.
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — With that said, the strtotime function consumes ~30% more memory and ~15% more execution time than the mktime function when both are passed [I]simple[/I] arguments.[/QUOTE]I can't comment on memory usage but the speed difference is not conclusive, from what I can see:[code=php]<?php
$date = "2006-02-28";

$time = microtime(true);
for ($x=0;$x<1000;$x++) :
$z = date("z", mktime(0,0,0,substr($date,5,2),substr($date,8,2),substr($date,0,4)))+1;
endfor;
echo "MKTIME(): ";
echo microtime(true)-$time;
echo "<br>";

$time = microtime(true);
for ($x=0;$x<1000;$x++) :
$z = date('z', strtotime($date))+1;
endfor;
echo "STRTOTIME(): ";
echo microtime(true)-$time;
echo "<br>";


$time = microtime(true);
for ($x=0;$x<1000;$x++) :
if (preg_match('/(d{4})-(d{2})-(d{2})/', $date, $t) && checkdate($t[2], $t[3], $t[1])) {
$z = date('z', strtotime($date))+1; // 337
} else {
$z = 'Invalid date argument.';
}
endfor;
echo "PREG_STRTOTIME(): ";
echo microtime(true)-$time;
echo "<br>";
?>[/code]
Live at:

http://www.nightshift58.com/webdev/test_speed_strtotime.php

What is astounding is the fact that the third test case is holding up its own quite well in spite of the fact that it does do quite a bit more...
Copy linkTweet thisAlerts:
@pcthugJan 27.2007 — Try bumping up the repetitional value up from 1000 to about 25000 as to get a larger an more accurate execution time. When I bump up the repetitional value to 25000 I got the following results:
<i>
</i>MKTIME(): 2.77290797234
STRTOTIME(): 3.21743979454
PREG_STRTOTIME(): 3.56660604477

MKTIME(): 2.85829911232
STRTOTIME(): 3.25503602028
PREG_STRTOTIME(): 3.5745909214

MKTIME(): 2.58041815758
STRTOTIME(): 3.24059786797
PREG_STRTOTIME(): 3.55887508392

Applying a little bit of mathematical logic, mktime() evalulates to be between 10-15% faster than strtotime() and preg_strtotime() evaulates to be ~10% slower.
Copy linkTweet thisAlerts:
@NightShift58Jan 27.2007 — MKTIME(): 2.8964829444885
STRTOTIME(): 2.993577003479
PREG_STRTOTIME(): 3.0321691036224

MKTIME(): 3.0608539581299
STRTOTIME(): 3.0620129108429
PREG_STRTOTIME(): 3.0916039943695

MKTIME(): 3.0666439533234
STRTOTIME(): 3.1512930393219
PREG_STRTOTIME(): 3.0708479881287

The differences I get aren't as marked as yours (server: Bravenet) but the trend is still there.
Copy linkTweet thisAlerts:
@pcthugJan 27.2007 — I'm executing locally, specs;

Software: Apache 2.2.3, PHP 5.2.0 [64M memory]

Hardware: 2.0GHz Intel Core 2 Duo, 1GB RAM
×

Success!

Help @gogelpot spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...