Page 1 of 1

date from week number

Posted: Sat May 03, 2008 10:05 am
by AXEmonster
hi guys

i have a search screen which passes a chosen year and week number.

how can i constitute a fully formated date from these 2 values to use within a SQL Server

Re: date from week number

Posted: Sat May 03, 2008 4:05 pm
by Kastor

Code: Select all

 
<?php
$time = mktime(0, 0, 0, $month, 0, $year);
$sql_date_time = date("Y-m-d H:i:s", $time);
$sql_date = date("Y-m-d", $time);
$sql_time = date("Y-m-d H:i:s", $time); 
?>

Re: date from week number

Posted: Sat May 03, 2008 6:43 pm
by AXEmonster
in your example how are you handling the WEEK NUMBER (1-52)?

i cant see the solution here?

Re: date from week number

Posted: Sat May 03, 2008 7:03 pm
by Zoxive

Code: Select all

date('W'); //18
Take a look at the manual for more information about the date function.

Re: date from week number

Posted: Sat May 03, 2008 7:11 pm
by AXEmonster
i have but couldnt find an answer

your example will produce the week number using the date() function. This i know and have already

maybe i wasnt clear. I appologise

i would like to take the following values (variables)

week no = 18
year =2006

now reconstitute a date from those two into (yyyy/mm/dd)

Re: date from week number

Posted: Sun May 04, 2008 4:18 am
by lafever
Ok think about this for a second... How many days are in a week? 7, right? Alright, so, you're going to have a function that determines how many weeks are in the year and a day to run off of for the starting date of the week. Then format it. Whether it be Monday or Sunday or whatever you use. What makes it complicated is you want to input the year also. So let's try this:

Code: Select all

 
function dateByWkYr($year, $week) {
    $first_day_of_yr = strtotime("1 January ".($year ? $year : date("Y")));
    // 10=sunday, 11=monday, 12=tuesday, etc. etc.
    // depending on what day of the week you want to start on
    // you can also add a $day in the function and search by the array
    $first_day       = (11 - date('w', $first_day_of_yr)) % 7 - 3;
    $day         = strtotime(($week - 1) . ' weeks '.$first_day.' days', $first_day_of_yr);
    return $day;
}
 
$year = date('Y');
$week = date('w');
$date = date('F j, Y, g:i a', dateByWkYr($year, $week));
echo 'week '.$week.' of '.$year.' begins on '.$date;
 
I had this code sitting around somewhere. Don't remember where it came from though. lucky you. :P

Re: date from week number

Posted: Sun May 04, 2008 4:50 am
by AXEmonster
cheers

will test it and let you know