Menu
Hi I have a date:
[code=php]
$date = “2006-12-03”;
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.
[code=php]
$date = "2006-12-03";
echo date('z', strtotime($date)); // 336
[/code]
[code=php]
$dateArray = explode('-',$date);
$year = $dateArray[0];
$month = $dateArray[1];
$day = $dateArray[2];
[/code]
[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]
[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]
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]
[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]
[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.... 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.
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.
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:<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
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
0.1.9 — BETA 5.18