date from week number

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
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

date from week number

Post 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
User avatar
Kastor
Forum Newbie
Posts: 24
Joined: Thu May 01, 2008 2:29 am
Location: Grodno, Belarus
Contact:

Re: date from week number

Post 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); 
?>
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: date from week number

Post by AXEmonster »

in your example how are you handling the WEEK NUMBER (1-52)?

i cant see the solution here?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: date from week number

Post by Zoxive »

Code: Select all

date('W'); //18
Take a look at the manual for more information about the date function.
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: date from week number

Post 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)
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: date from week number

Post 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
AXEmonster
Forum Commoner
Posts: 34
Joined: Fri Sep 05, 2003 11:27 am
Location: newcastle UK

Re: date from week number

Post by AXEmonster »

cheers

will test it and let you know
Post Reply