Link to home
Start Free TrialLog in
Avatar of hinkeltje
hinkeltje

asked on

How to get weeknumbers from a date-serial number

Hi,

I have a variable with a date serial in it.
For example: Friday, July 13 8:00 AM is in serial 995004000.
What I would like is to get the weeknumber out of this serial number. And Friday, July 13th is weeknumber 28.
Is there someone who can help me solving this problem?

Thanks,
Hinke
Avatar of ykf2000
ykf2000

how do you get 995004000 from Friday, July 13 8:00 AM? is that a unix timestamp?
Avatar of hinkeltje

ASKER

Yes it is.
Yes it is.
try this:

<?
     $your_time = 995004000;
     $day = date("j",$your_time);
     $month = date("n",$your_time);
     $year = date("Y",$your_time);
     $total_days = $day;
     
     for($i=1;$i<=$month;$i++)
     {
          $total_days = $total_days + date("t",mktime (0,0,0,$day,$month,$year));
     }

     $number_of_weeks = $total_days / 7;
?>
It's not giving me the correct answer.
$number_of_weeks has result 32.857142857143
But 995004000 is in week 28.
OK sorry some technical mistake. This should work very fine.

<?
     $your_time = 995004000;
     $day = date("j",$your_time);
     $month = date("n",$your_time);
     $total_days = $day;
     
     for($i=1;$i<$month;$i++)
     {
          $total_days = $total_days + date("t",mktime (0,0,0,$i,$day,$year));
     }
     $number_of_weeks = round($total_days / 7);
     echo $number_of_weeks;
?>
Ok, this is giving me the correct result back for Friday 13 July.
But if I give Mon 9th July, it gives me week 27, but Mon 9th July is still week 28.
It seems that your week ends on wednesday and starts on thursday. But (ofcourse) it need to start on mondays and ending on sundays.
ok try this again: :)


<?
     $your_time = mktime (0,0,0,7,1,2001);
     $day = date("j",$your_time);
     $month = date("n",$your_time);
     $total_days = $day;
     
     for($i=1;$i<$month;$i++)
     {
          $total_days = $total_days + date("t",mktime (0,0,0,$i,$day,$year));
     }
     $remainder = ($total_days / 7) - round($total_days / 7);
     $number_of_weeks = round($total_days / 7);
     if($remainder > 0 && $remainder < 0.5)
          $number_of_weeks = $number_of_weeks + 1;
     echo $number_of_weeks;
?>
I think we are almost there :-)
But your week starts at sunday, and I want my week to start on monday.

So for example:
week 27  mon July 2nd till sun July 8th
week 28  mon July 9th till sun July 15th
week 29  mon July 16th till sun July 22nd
ok gotcha:


<?
     $your_time = mktime (0,0,0,7,22,2001);
     $day = date("j",$your_time);
     $month = date("n",$your_time);
     $total_days = $day;
     
     for($i=1;$i<$month;$i++)
     {
          $total_days = $total_days + date("t",mktime (0,0,0,$i,$day,$year));
     }
     $remainder = ($total_days / 7) - round($total_days / 7);
     $number_of_weeks = round($total_days / 7);
     
     if($remainder > 0.15 && $remainder < 0.65)
          $number_of_weeks = $number_of_weeks + 1;
     echo $number_of_weeks;
?>
Thanks, this seems to work. But.... you use:
mktime (0,0,0,7,15,2001)
And I am using: 995004000
And if I use the integer above, it gives me back week 28 all the time.
That number is a constant and sure enough it gives 28 all the time because that number represents 13th July 2001 which is the 28th week.
ASKER CERTIFIED SOLUTION
Avatar of ykf2000
ykf2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah duh... and if I change the number (let's say add an extra 10 days) it still gives me back week 28.
what do u mean add another 10 days? You can't just add 10 to that number. It does not work that way.
I can add an extra 10 days to that number :-)

time()+(86400*10)
It is representing today plus an extra 10 days.

And 86400 comes from 86400 seconds in one day: 24 * 60 * 60
Thank you ykf2000 for your help (and time).
It works perfect now.
great :)