Jump to content

date range


jimfog

Recommended Posts

$now = new DateTime($newdate);$today=$now->format('j m Y');$startDate = $mondayweek->modify("tomorrow")->modify("last Monday")->format('j m Y');$endDate =  $mondayweek->modify("yesterday")->modify("next Sunday")->format('j m Y');

now() is the date you want to identify the startdate and enddate (monday to sunday), $today is the date but in specific format, therefore in the if condition using $today as it is

if($today>= $startDate && $today <= $endDate)

will always be true! hence repeated id ref, you need the variable applied to td cell increase by 1, and compare this variable value in the 'if' condition

Edited by dsonesuk
Link to comment
Share on other sites

Ι've managed to pull it off-partially. Instead of targeting the tr element I targeted the td element(which contains the number corresponding to the day of the month) and instead of using $today i used $datecount which is the variable the prints the days of the month. Look at the image and the code: https://skydrive.live.com/redir.aspx?cid=be27434b2aac8130&resid=BE27434B2AAC8130!232&parid=BE27434B2AAC8130!140

if($datecount>= $startDate && $datecount <= $endDate)				   if((date("d") == $datecount)&& (date("n") == (date("n", strtotime($newdate)))))				    { echo "<td class='current-day-highlited' align='center' valign='middle' height='20px'>" .$datecount. "</td>\n";}				    else 				    {echo "<td class='other' align='center' valign='middle' height='20px'>".$datecount."</td>\n";}			 

The inner if statement just highlights the current day within the current week.

Link to comment
Share on other sites

Yes you have to use $datecount, but because the value will be the previous days value you would increase it by 1, which will make it fall between $startDate and $endDate, and don't forget you have to compare it not by just the day, but month and year also in format of "Ymd", then echo the tr element with id.

Link to comment
Share on other sites

Yes you have to use $datecount, but because the value will be the previous days value you would increase it by 1,which will make it fall between $startDate and $endDate,
Why that, i am confused,can you elaborate please a little
and don't forget you have to compare it not by just the day, but month and year also in format of "Ymd", then echo the tr element with id.
I am confused here too, maybe if you write some code i will be able to understand it better. Either way there still is a problem when highlighting the days which refer to the days that belong to previous/next month. Formatting does not take place there, see image:https://skydrive.live.com/redir.aspx?cid=be27434b2aac8130&resid=BE27434B2AAC8130!233&parid=BE27434B2AAC8130!140 Edited by jimfog
Link to comment
Share on other sites

A generic error you seem to be making is that you keep returning to strings.Rule of thumb: Just when you're about to output a DateTime object (like, at the very "echo" line), use format(). Do not use format() anywhere else. The result of format() is a string... a string that is meaningless in PHP. Only the object has a meaning, in turn allowing you to do meaningful stuff like comparing said date with another one.

Link to comment
Share on other sites

Where exactly is the mistake in the code above? Just give me an example-in theory I understand what are you talking about. Are you referring to the $datecount variable?

Edited by jimfog
Link to comment
Share on other sites

<?php  $datecount = 6;$mon = 5; $year = 2012; 				/// idenify start and end week days using value supplied				$newdate="13-05-2012";				$now = new DateTime($newdate);				$today=$now; 				$startDate = $today->modify("tomorrow")->modify("last Monday")->format('Ymd');				$endDate =  $today->modify("yesterday")->modify("next Sunday")->format('Ymd');				/// End of idenify start and end week days using value supplied echo 'value of startDate '.$startDate.'<br>';echo 'value of endDate '.$endDate.'<br><br>'; echo ' current value of $datecount as it loops through days '.$datecount.'<br>';echo ' current value of $mon as it loops through months '.$mon.'<br>';echo ' current value of $year as it loops through months '.$year.'<br><br>'; echo 'you are comparing '.$startDate.' and '.$endDate.' with '.$datecount.'<br><br>'; ///convert $datecount$convert_datecount = new DateTime($datecount.'-'.$mon.'-'.$year);$convert_datecount = $convert_datecount->format('Ymd');/// end convert $datecount echo 'correct format of $datecount as $convert_datecount '.$convert_datecount.' but this value will be the LAST days value of last week, as it is yet to be increased for next row, when a new <TR> row is started, So you increase this value to pre-empt this.<br><br>'; ///convert $datecount$convert_datecount = new DateTime($datecount.'-'.$mon.'-'.$year);$convert_datecount = $convert_datecount->modify("tomorrow")->format('Ymd');/// end convert $datecount echo 'correct format of $datecount as $convert_datecount '.$convert_datecount.' with correct date to compare in if condition.<br><br>'; echo 'you are Now comparing '.$startDate.' and '.$endDate.' with '.$convert_datecount.'<br><br>';?>

Edited by dsonesuk
Link to comment
Share on other sites

give me a little help please,what are you trying to do here? Are you demonstrating a new code for looping the days of a month, different from the one you have given me the other days? Or is it sth else-just an answer to what the previous post was claiming? And if you are looping where is the for loop?

Link to comment
Share on other sites

This is the for loop from my original script but from your end its probably changed a lot, so i was reluctant to show it, But! just how you get the correct value of $datecount for the if condition to work.

				 for($date=1;$date<($totaldays_in_mon+$startdate);$date++)					{					$colcout++;										if($colcout ==1)						{// <tr> is only added when colcount = 1 meaning fiirst new <td> ie  if new column add <tr> before it // Note: $mon and $year should be defined further up unless you changed it then your screwed, and have to change them $convert_datecount = new DateTime($datecount.'-'.$mon.'-'.$year);$convert_datecount = $convert_datecount->modify("tomorrow")->format('Ymd'); //get start and end date of specific date value $newdate$now = new DateTime($newdate);$today=$now;$startDate = $today->modify("tomorrow")->modify("last Monday")->format('Ymd');$endDate =  $today->modify("yesterday")->modify("next Sunday")->format('Ymd'); 				   	 if($convert_datecount>= $startDate && $convert_datecount<= $endDate)							{							echo '<tr id="current_week">';							}						else							{							echo '<tr>';							}						}					if($date>=$startdate)						{						$datecount++;						if($colcout==6 || $colcout==7)							{							$cellclass="weekend";							}						else							{							$cellclass="weekdays";							}						?>						<td class="<?php echo $cellclass; ?>" id="<?php echo 'dateid_'.$year.str_pad(date("m", $month), 2 , "0", STR_PAD_LEFT).str_pad($datecount, 2 , "0", STR_PAD_LEFT); ?>"><?php echo $datecount; ?></td>				  						  <?php						}						elseif($colcout==6 || $colcout==7)						{						?>

IF you wish to change td class, the code is basically the same except for increasing $datecount variable value, as you are dealing with $datacount as it is used to apply the dates, and you need to check against the specific date between and including mon, sunday to highlight (add border)

     if($date>=$startdate)                        {                        $datecount++;                        if($colcout==6 || $colcout==7)                            {                            $cellclass="weekend";                            }                        else                            {                            $cellclass="weekdays";                            } 					   $convert_datecount = new DateTime($datecount.'-'.$mon.'-'.$year);					    $convert_datecount = $convert_datecount->format('Ymd');					   $today= $today->format('Ymd'); //if datecount ($compare_today) between startdate/enddate and not equal to $newdate value($today) overwrite current class ref and apply highlight cell class instead                       if($compare_today >= $startday_mon && $compare_today<= $enday_sun && $compare_today != $today)                            {                        $cellclass="highlight_weekdays";                            }					    else  //else datecount ($compare_today) between startdate/enddate and equal to $newdate value($today) overwrite current class ref and apply highlight cell class and also border class							  {							   $cellclass="highlight_weekdays todays_border";							  }                                                    ?>                        <td class="<?php echo $cellclass; ?>" id="<?php echo 'dateid_'.$year.str_pad(date("m", $month), 2 , "0", STR_PAD_LEFT).str_pad($datecount, 2 , "0", STR_PAD_LEFT); ?>"><?php echo $datecount; ?></td>                                            <?php                        }

this is not tested i just copied and pasted in original partially copied code. and as i said its probably no resemblance to what you have.

Edited by dsonesuk
Link to comment
Share on other sites

Do you believe the above code is better than that you have given me the other days? I am assuming the reason you changed it is for including the week highlighting feature I need. Correct?

Link to comment
Share on other sites

No! i'm just showing how you will need to convert $datecount to use in the if condition, I don't care which method you use, in BOTH cases you WILL have to convert $datecount to include month and year values, and the format it 'Ymd', because $datecount will only equal a value 1 to 31 at most, while in the if condition startdate and endate will be for example 20120507 and 20120513, if you don't include month and year, it will highlight in all months the week with $datecount value within it

Link to comment
Share on other sites

Where exactly is the mistake in the code above? Just give me an example-in theory I understand what are you talking about. Are you referring to the $datecount variable?
I was referring to what dsonesuk had explained, with examples, before me:
$now = new DateTime($newdate);//$now is meaningful$today=$now->format('j m Y');//$today is meaningless$startDate = $mondayweek->modify("tomorrow")->modify("last Monday")->format('j m Y');//$startDate is meaningless$endDate =  $mondayweek->modify("yesterday")->modify("next Sunday")->format('j m Y');//$endDate is meaningless//Later...if($today>= $startDate && $today <= $endDate) //You're checking if one meaningless string is greater than another meaningless string (and because it's meaningless... what does "greater than" mean?), and if another meaningless string is less than another meaningless string...

Also (and I'm not sure if this is related to your issue above, but just in case), if you want to preserve a copy of a date (so that you can refer back to it for comparisons, etc.), you need to clone it. Otherwise, you're always working with your original date. e.g.

$now = new DateTime();$today = $now;$now->modify('+1 day');//You may think you're only modifying $now, but you're actually modifying $today too.echo $now->format('d-m-Y');//Outputs 14-05-2012echo $today->format('d-m-Y');//Outputs 14-05-2012 as well$notToday = clone $today;$notToday->modify('+1 day');//You're only modifying $notTodayecho $notToday->format('d-m-Y');//Outputs 15-05-2012echo $now->format('d-m-Y');//Still outputs 14-05-2012echo $today->format('d-m-Y');//Still outputs 14-05-2012 as well

Link to comment
Share on other sites

I think I got your point about "meaningless strings". My assumption was that

$today=$now->format('j m Y');

will suffice cause $now( which is a date object) is assigned to $today and that is that. I did not know that the above was wrong.Furthermore I assume, now, that the below code is correct:

$startDate = $today->modify("tomorrow")->modify("last Monday")->format('Ymd');$endDate =  $today->modify("yesterday")->modify("next Sunday")->format('Ymd');

...because $today contains a date object. Am I saying it correctly?

Link to comment
Share on other sites

It is "correct" in the sense that $startDate and $endDate will end up containing meaningless strings and no error will appear on screen. You can output those strings, or supply them as inputs to something else (e.g. MySQL)... But they're useless for anything within PHP, such as comparisons or iterations.In your real code, you later have comparisons, so this makes the above "not correct". You need to take format() out of the picture:

$startDate = $today->modify("tomorrow")->modify("last Monday");$endDate =  $today->modify("yesterday")->modify("next Sunday");

Also, keep in mind that modify() modifies and returns the object that called it. To put this in another way, after the line

$startDate = $today->modify("tomorrow")->modify("last Monday");

$today (as well as $startDate) is now pointing to the last Monday. Whatever the value of $today was previously, it is now lost, hence my point about "clone".

Link to comment
Share on other sites

Hang-on, are you saying that in a case where you would use these as

echo $startDate+$endDate;if ($startDate > $endDate){echo 'startDate is greater';}else{echo 'EndDate is greater';}

it won't work? i can understand when formatted as ('j m Y') because have a space and (Ymj') would give a false comparison as j produces 1 to 9 without leading zeros, but when formatted as ('"Ymd") as far as i know it is now treated as a integer value and acts as so. When i first did this i used the actual timestamp in the if condition, and only converted to the datetime format used by jimfog, but it still worked.

Link to comment
Share on other sites

The timestamps (that is, those returned by getTimestamp()) are continuous integers from a common offset, so comparisons and differences alike would work. But with strings...

echo '20120201'-'20120131';

outputs

70

When the right answer (if using DateTime::diff()) is instead 1.As far as comparisons go... in this particular format ("Ymd") it should work, but still - a string is simply not meant for this.

Link to comment
Share on other sites

The problem doesn't stem from the string itself, but the fact the string is meaningless in this context. To give it some meaning, PHP blindly turns it into the baseless (as in "non part of a continuous series") integer 20120201 (analogously with the other string), which in turn produces the integer 70 in the end. intval() will simply make PHP explicitly turn the string into an integer... the exact same integer, and therefore the exact result.Timestamps work as integers because are continuous... they fill the whole range from 0 to 4294967295 with values that be mapped to something (although they're defined as the seconds since 1970, they could represent any sort of duration, as long as you correct for 1970 in your calculations), whereas 20120299 is not a valid integer in the "Ymd" scheme.

Link to comment
Share on other sites

I tried dsonesuk's code and the problem is that ALL the rows are highlighted and not just the one corresponding to the current week:

$cmonth = date("m") ;$cYear = date("Y");$convert_datecount = new DateTime($datecount.'-'. $cmonth .'-'.$cYear );$convert_datecount = $convert_datecount->modify("tomorrow")->format('Ymd');$today=$now;$startDate = $today->modify("tomorrow")->modify("last Monday")->format('Ymd');$endDate =  $today->modify("yesterday")->modify("next Sunday")->format('Ymd');for ($date = 1; $date < ($totaldays_in_mon + $startdate); $date++) {						 $colcout++;											    if ($colcout == 1) {										    if((isset($_GET['weekview']))&&($convert_datecount>= $startDate && $convert_datecount<= $endDate))								  echo  '<tr class="other" >';			    else				    echo  '<tr class="john" >';					   									  }				   					if ($date >= $startdate) {				   $datecount++;                  									 }		  if ($colcout == 6 || $colcout == 7) {					   $cellclass = "weekend";				   } else {					   $cellclass = "weekdays";				   }				  		{echo "<td class=".$cellclass.'id=dateid_' . $year . str_pad(date('m', $month), 2, '0', STR_PAD_LEFT)				    . str_pad($datecount, 2, '0', STR_PAD_LEFT).'>'.$datecount."</td>";}																											 						  if ($colcout % 7 == 0) {								 $colcout = 0;								 $rowcount++;								}

Here is the link so you can see https://skydrive.live.com/redir.aspx?cid=be27434b2aac8130&resid=BE27434B2AAC8130!234&parid=BE27434B2AAC8130!140 I will have to use a different strategy but dsonesuk can tell better if I did sth wrong in the code above and it does not work as expected

Link to comment
Share on other sites

NEVERMIND, IT WORKS, MANY THANKS TO YOU TWO, This logic is new to me and I might come back for questions-as part of the learning process though.

Link to comment
Share on other sites

What was the problem?BTW, I don't know if this was the cause of your problem, but I noticed

echo "<td class=".$cellclass.'id=dateid_' . $year . str_pad(date('m', $month), 2, '0', STR_PAD_LEFT)                                    . str_pad($datecount, 2, '0', STR_PAD_LEFT).'>'

You're missing a space here... and should probably add quotes around the attributes too, so that you have

echo '<td class="'. $cellclass . '" id="dateid_' . $year . str_pad(date('m', $month), 2, '0', STR_PAD_LEFT)                                    . str_pad($datecount, 2, '0', STR_PAD_LEFT).'">'

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...