Page 1 of 1

[SOLVED] getting weeknumber from date

Posted: Thu Nov 02, 2006 1:26 am
by ryuuka
o/

the problem is this i want to build a script that takes the time out of the database
and converts it to a different value.
now this would normally not be a problem but i want the weeknumber
(say week 1 to 52)
the problem here is that the only dates i have are like this: 01-08-2006 (the date is from a database)
i need a week number for this what i have is the following:

Code: Select all

$weeknr = date("W", strtotime($s_date));
echo "$weeknr<br>";
      
echo $s_datum;
the resullt for this is:
01
3-8-2006

W stands for the weeknumber
and the strtotime is supposed to disect the date

what am i doing wrong and can i get a pointer in the right direction

Posted: Thu Nov 02, 2006 2:03 am
by aaronhall
strtotime is probably returning false because of an invalid date format. strtotime may need MM-DD-YYYY format to work.

Posted: Thu Nov 02, 2006 2:24 am
by timvw
Why don't you simply select the weeknumber in the database? (Check the date and time functions of the sql dbms you're using...)

Posted: Thu Nov 02, 2006 4:31 am
by ryuuka
the problem of getting the weeknumber out of the database is the fact that it isn't there
i need this script to input that into the database
we have a few weeknumbers in the database but for the most part it's just NULL

Posted: Thu Nov 02, 2006 4:42 am
by volka
Of what type it the date field in the database? What database system is it?

Posted: Thu Nov 02, 2006 5:03 am
by ryuuka
the date/Datum field is a datetime field and the weeknummer field is an interger

the database is a mssql and i use php(as viewable in my sig

Posted: Thu Nov 02, 2006 5:06 am
by volka
Take a look at the Datepart(,) function of mssql

Posted: Thu Nov 02, 2006 6:04 am
by ryuuka
ah thanks the datepart did the trick

week is weeknumber and datum is the date table
this query is only for the 2006-9-3 date

Code: Select all

SELECT     DATEPART(week, datum) AS weeknumber
FROM         @@@_printer_stats
WHERE     (naam = 'PR0151') AND (datum = '2006-9-3')
thanks a lot