kamal213 Posted November 24, 2011 Share Posted November 24, 2011 Hi guys! This will seem complex but I really need your help If a have a variable with a date value (i.e. $a = '23/10/2011') How do you work out the week number of that date? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/251722-php-calculating-the-week-number-for-a-given-date/ Share on other sites More sharing options...
gristoi Posted November 24, 2011 Share Posted November 24, 2011 <?php $a = '23/10/2011'; $dateStamp = strtotime($a); $week = date('W', $dateStamp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/251722-php-calculating-the-week-number-for-a-given-date/#findComment-1290930 Share on other sites More sharing options...
JonnoTheDev Posted November 24, 2011 Share Posted November 24, 2011 That's not going to work without the date in the correct format. (23/10/2011 will result in week 01, should be 42). You require the date as follows: YYYY-MM-DD <?php $a = '2011-10-23'; $week_num = date('W', strtotime($a)); echo $week_num; ?> Use the php manual to find answers http://php.net/date Quote Link to comment https://forums.phpfreaks.com/topic/251722-php-calculating-the-week-number-for-a-given-date/#findComment-1290933 Share on other sites More sharing options...
gristoi Posted November 24, 2011 Share Posted November 24, 2011 thanks for the correction neil. Schoolboy error on my behalf. kamal213, if you need to convert uk into a week number then one option is: <?php list($day,$month,$year) = explode('/','15/11/2011'); $weekNumber = date('W',mktime(0,0,0,$month,$day,$year)); echo $weekNumber; ?> cheers Quote Link to comment https://forums.phpfreaks.com/topic/251722-php-calculating-the-week-number-for-a-given-date/#findComment-1290935 Share on other sites More sharing options...
jcbones Posted November 24, 2011 Share Posted November 24, 2011 Date Time formats accepted by strtotime() function (or most any PHP functions requiring it) Quote Link to comment https://forums.phpfreaks.com/topic/251722-php-calculating-the-week-number-for-a-given-date/#findComment-1290937 Share on other sites More sharing options...
kamal213 Posted November 24, 2011 Author Share Posted November 24, 2011 gristoi if you were a chick i'd marry you right now! lol You last post is exactly what I was looking for! Thanks aswell neil.johnson especially for correcting gristoi's error but for now I need the uk date format. Thanks guys for your help I taught I would be really complicated but you both made it look simple. Problem Solved! Quote Link to comment https://forums.phpfreaks.com/topic/251722-php-calculating-the-week-number-for-a-given-date/#findComment-1290938 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.