Jump to content

PHP Week number


kamal213

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/238859-php-week-number/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227341
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227354
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227361
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227379
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/238859-php-week-number/#findComment-1227413
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.