kamal213 Posted June 9, 2011 Share Posted June 9, 2011 Hi guys, I have the following block of code below which gets today's date, month, week and year. if(isset($_GET['day'])){ $day = $_GET['day']; } else{ $day = date("j"); $day = "0".$day; } if(isset($_GET['month'])){ $month = $_GET['month']; } else{ $month = date("n"); $month = "0".$month; } if(isset($_GET['year'])){ $year = $_GET['year']; } else{ $year = date("Y"); } if(isset($_GET['week'])){ $week = $_GET['week']; } else{ $week = date("W"); } I at the moment I can echo $week; which gives me the week number of the year (23). However I would like it to also tell me the From and To Date (i.e. Week Number 23: From 06/06/2011 To 12/06/2011 and so on). Please I need your help and would appreciate your ideas. Thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/238859-php-week-number/ Share on other sites More sharing options...
kamal213 Posted June 9, 2011 Author Share Posted June 9, 2011 Also I use this code below to echo today's date if that help. Please let me know if you need more info: echo $day."/".$month."/".$year; Quote Link to comment https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227324 Share on other sites More sharing options...
TeNDoLLA Posted June 9, 2011 Share Posted June 9, 2011 What defines that To date? Not sure what you are trying to achieve but you can add dates to the current date like this: date('Y-m-d', strtotime('+7 day')); Quote Link to comment https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227337 Share on other sites More sharing options...
kamal213 Posted June 9, 2011 Author Share Posted June 9, 2011 Thanks TenDoLLa I am using this $prevDayTimestamp = strtotime("$day-$month-$year -7 day"); $nextDayTimestamp = strtotime("$day-$month-$year +7 day"); This creates the previous and next week. I then echo a previous and next link as well as $week; which means when you click on the link it takes you to the next week/previous week and so on. So at the moment we are in week 23, if i click on the next link it goes to week 24 - this works fine. Basically its I would like it to also tell me the from and to date when i click on the next/previous link i.e. Week 23: From 06/062011 To 12/06/2011..something like that. Quote Link to comment https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227341 Share on other sites More sharing options...
TeNDoLLA Posted June 9, 2011 Share Posted June 9, 2011 You can get the start and end days for current week by playing with date functions and some simple mathematics. For example: $startDateOfWeek = date('d/m/Y', strtotime('- ' . (7 - date('w')) . ' days')); // 06/06/2011 $endDateOfWeek = date('d/m/Y', strtotime('+ ' . (7 - date('w')) . ' days')); // 12/06/2011 Quote Link to comment https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227354 Share on other sites More sharing options...
kamal213 Posted June 9, 2011 Author Share Posted June 9, 2011 Thanks TeNDollA Just tried it but getting some errors:(Notice: A non well formed numeric value encountered in C:\xampp\htdocs\site\test.php on line 11 ). Probably coz of the way my code is written. This is exactly what am doing: <?php function diaryHrefFromTimestamp($timestamp) { $day = date('d', $timestamp); $month = date('m', $timestamp); $year = date('Y', $timestamp); $week = date('W', $timestamp); return "canvasserweekstats.php?&day={$day}&month={$month}&week={$week}&year={$year}"; } //Get vars from URL if(isset($_GET['day'])){ $day = $_GET['day']; } else{ //Get today's date and put them in date, month, year $day = date("j"); $day = "0".$day; } if(isset($_GET['month'])){ $month = $_GET['month']; } else{ $month = date("n"); $month = "0".$month; } if(isset($_GET['year'])){ $year = $_GET['year']; } else{ $year = date("Y"); } if(isset($_GET['week'])){ $week = $_GET['week']; } else{ $week = date("W"); } //Create timestamps for prev/next days $prevDayTimestamp = strtotime("$day-$month-$year -7 day"); $nextDayTimestamp = strtotime("$day-$month-$year +7 day"); //Create href values for prev/next links $prevDayHref = diaryHrefFromTimestamp($prevDayTimestamp); $nextDayHref = diaryHrefFromTimestamp($nextDayTimestamp); echo "<a href='{$prevDayHref}'><img src='images/previous.gif' alt='' /></a>\n"; echo" ";echo" "; echo "<a href='{$nextDayHref}'><img src='images/thenext.gif' alt='' /></a>\n"; echo" ";echo" ";echo" "; echo ' Week'; echo $week; ?> Could you tell me how you code would fit into it. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227361 Share on other sites More sharing options...
TeNDoLLA Posted June 9, 2011 Share Posted June 9, 2011 I dont get any errors for the code you posted. Can you just post that line 11 what gives the error? I think you are providing for the date() function a string instead of integer type as a second parameter. Make sure you are passing INT type as 2nd parameter for date everywhere. Quote Link to comment https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227379 Share on other sites More sharing options...
kamal213 Posted June 9, 2011 Author Share Posted June 9, 2011 Thisn is line 11 $day = date('d', $timestamp); Could you try the following script below from your end to see if it works/if am on the right track <?php function diaryHrefFromTimestamp($timestamp) { $day = date('d', $timestamp); $month = date('m', $timestamp); $year = date('Y', $timestamp); $week = date('W', $timestamp); return "test.php?&day={$day}&month={$month}&week={$week}&year={$year}"; } //Get vars from URL if(isset($_GET['day'])){ $day = $_GET['day']; } else{ //Get today's date and put them in date, month, year $day = date("j"); $day = "0".$day; } if(isset($_GET['month'])){ $month = $_GET['month']; } else{ $month = date("n"); $month = "0".$month; } if(isset($_GET['year'])){ $year = $_GET['year']; } else{ $year = date("Y"); } if(isset($_GET['week'])){ $week = $_GET['week']; } else{ $week = date("W"); } //Create timestamps for prev/next days $prevDayTimestamp = date('$day-$month-$year', strtotime('- ' . (7 - date('w')) . ' days')); // 06/06/2011 $nextDayTimestamp = date('$day-$month-$year', strtotime('+ ' . (7 - date('w')) . ' days')); // 12/06/2011 //Create href values for prev/next links $prevDayHref = diaryHrefFromTimestamp($prevDayTimestamp); $nextDayHref = diaryHrefFromTimestamp($nextDayTimestamp); echo "<a href='{$prevDayHref}'><img src='images/previous.gif' alt='' /></a>\n"; echo" ";echo" "; echo "<a href='{$nextDayHref}'><img src='images/thenext.gif' alt='' /></a>\n"; echo" ";echo" ";echo" "; echo $day."/".$month."/".$year; ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227413 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.