Jump to content

date range


jimfog
 Share

Recommended Posts

I want to take the dated between 2 dates that lie far left and far right of a specific date interval. For example i want to dates between 4-30-2012 and 5-5-2012. I found in the web the following code which works:

$startDate = '4-30-2012';$endDate ='5-5-2012';for($current = $startDate ; $current != $endDate ; $current = date('d-m-Y', strtotime("$current +1 day")))   {echo $current.'<br>';

The problem is that i cannot do the above code work if instead of using date strings i use methods of the datelclass such as $monday->format('Y m d') or $sunday->format('Y m d') which both output date strings, Basically here is what i was trying to achieve:

$startDate =  [b]$monday->format('Y m d') [/b];$endDate = $[b]sunday->format('Y m d') [/b]';for($current = $startDate ; $current != $endDate ; $current = date('d-m-Y', strtotime("$current +1 day")))   {echo $current.'<br>';

What do you think? How am i going to take the date range between $monday->format('Y m d') and $sunday->format('Y m d') ? I think, that he above code does not work because strtotime cannot be used with methods such as these of the dateclass shown here.

Link to comment
Share on other sites

Your second example needs to follow the same date format. So try this:

$startDate = $monday->format('d-m-Y');$endDate = $sunday->format('d-m-Y')';

Also, I don't know if you did this by accident but the for loop is missing the closing curly bracket }

Edited by Err
Link to comment
Share on other sites

You don't need to loop through current weekdays, just identify if current day falls between start - end dates, and apply id to tr element

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><style type="text/css">.mytable td.weekdays {background-color:#FFFF99;}.mytable td.weekend {background-color: #FFCC99;}.mytable #current_week td.weekdays, .mytable #current_week td.weekend  {background-color:#FF0033;}</style></head><body><?php$current = new DateTime;$monday   = new DateTime;$sunday   = new DateTime;$current = $current->format('Ymd'); // change from today to//$current = $current->modify("-1 week")->format('Ymd'); // last week$startDate = $monday->modify("tomorrow")->modify("last Monday")->format('Ymd');$endDate =   $sunday->modify("yesterday")->modify("next Sunday")->format('Ymd');echo 'Monday = '.$startDate.'<br />';echo 'Sunday = '.$endDate.'<br />';echo 'current = '.$current.'<br />';?><table class="mytable"><tr><td class="weekdays">23</td><td class="weekdays">24</td><td class="weekdays">25</td><td class="weekdays">26</td><td class="weekdays">27</td><td class="weekend">28</td><td class="weekend">29</td></tr><?phpif($current >= $startDate && $current <= $endDate){echo '<tr id="current_week">';}else{echo '<tr>';}?><td class="weekdays">30</td><td class="weekdays">1</td><td class="weekdays">2</td><td class="weekdays">3</td><td class="weekdays">4</td><td class="weekend">5</td><td class="weekend">6</td></tr></table></body></html>

Link to comment
Share on other sites

If you're willing to sacrifice 5.2 compatibility (i.e. make your app require a minimum of 5.3.0), you can use the DatePeriod class. What it does is that it takes two dates, and generates an iterator between them over a period. You could loop over all months between two dates, all weeks between dates, every week and a few days between dates... or in your case, all days between two dates.So, to get get an iterator for all dates within a week:

foreach (new DatePeriod($monday, new DateInterval('P1D'), $sunday) as $day) {/* $day will contain a DateTime representing each day between Monday and Saturday.If you want to include Sunday, add one day to $sunday before the loop.*/}

If you want to include PHP 5.2.2 as well, you could use something like this:

for ($day = (clone)$monday; $current < $sunday; $day->modify('+1 day')) {/* $day will contain a DateTime representing each day between Monday and Saturday.If you want to include Sunday, add one day to $sunday before the loop OR use "<=" instead of "<".*/}

Link to comment
Share on other sites

You don't need to loop through current weekdays, just identify if current day falls between start - end dates, and apply id to tr element .....................
Regarding your code Dsonesuk i will see it later-i want to see first boen_robot's code(for educational purposes.)
If you want to include PHP 5.2.2 as well, you could use something like this:
for ($day = (clone)$monday; $current < $sunday; $day->modify('+1 day')) {/* $day will contain a DateTime representing each day between Monday and Saturday.If you want to include Sunday, add one day to $sunday before the loop OR use "<=" instead of "<".*/}

I tried the above code and the browser outputs that there is syntax error: Parse error: syntax error, unexpected ')' in C:\xampp\htdocs\korakeiko\timetest3.php on line I then tried for ($day = clone $monday; $current < $sunday; $day->modify('+1 day')) and i got the message that Catchable fatal error: Object of class DateTime could not be converted to string in C:\xampp\htdocs\korakeiko\timetest3.php on line Finally i tried for ($day = clone $monday->format('d-m-y'); $current < $sunday->format('d-m-y') ; $day->modify('+1 day')) abd then i got Fatal error: __clone method called on non-object in C:\xampp\htdocs\korakeiko\timetest3.php on line 16 There must be a problem with the object definition,i tried some things but i would appreciate some help.
Link to comment
Share on other sites

I keep forgetting that clone is a keyword, and not a type :facepalm: (I don't do much cloning...).And I also forgot I switched between variable names, so... khmm... try it like this:

for ($day = clone $monday; $day < $sunday; $day->modify('+1 day')) {}

(notice the middle part of the "for")

Link to comment
Share on other sites

I keep forgetting that clone is a keyword, and not a type :facepalm: (I don't do much cloning...). And I also forgot I switched between variable names, so... khmm... try it like this:
for ($day = clone $monday; $day < $sunday; $day->modify('+1 day')) {}

(notice the middle part of the "for")

It does not work, i get the same message:Catchable fatal error: Object of class DateTime could not be converted to string in C:\xampp\htdocs\korakeiko\timetest3.php on line 14 Here is the whole code:
 $now = new DateTime;$week = $now->format('d-m-y') === 'Sunday' ? 'previous' : 'this';$monday = clone $now;$monday->modify('Monday ' . $week . ' week'); $sunday = clone $now;$sunday->modify('Sunday ' . $week . ' week');

I cannot understand why $day->modify('+1 day')) cannot be converted to a string.

Edited by jimfog
Link to comment
Share on other sites

The error seems to be caused by the echo, not the modify().You need to explicitly call "format" upon echoing, like:

$now = new DateTime;$monday = clone $now;$sunday = clone $now;for ($day = clone $monday; $day < $sunday; $day->modify('+1 day')) {echo $day->format('Y-m-d');}

But that's shouldn't even enter the loop once, since $day will always be equal to $sunday rather than less than it.You'd have to modify $sunday, so that it's later than $monday, like:

$now = new DateTime;$monday = clone $now;$sunday = clone $now;$sunday->modify('+7 days');for ($day = clone $monday; $day < $sunday; $day->modify('+1 day')) {echo $day->format('Y-m-d');}

Link to comment
Share on other sites

$now could be tuesday date, wednes.....etc therefore so will $monday, $sunday you have to define the actual monday and sunday values to get the range to loop through

<?php$now = new DateTime;//$now->modify("+1 day");//$now->modify("+2 days");//$now->modify("+3 days");//$now->modify("+4 days");//$now->modify("+5 days");//$now->modify("+6 days");//$now->modify("+7 days"); $monday = clone $now;$monday->modify("+1 day")->modify("last Monday"); $sunday = clone $now;$sunday->modify("-1 day")->modify("next Sunday"); for ($day = clone $monday; $day <= $sunday; $day->modify('+1 day')) {echo $day->format('Y-m-d').'<br />';}?>

the +1 and -1 days are used if on Monday, the last monday would the Monday week before, by going forward 1 day and selecting last Monday it becomes current last Monday, and then do reverse for Sunday.

Edited by dsonesuk
Link to comment
Share on other sites

The error seems to be caused by the echo, not the modify(). You need to explicitly call "format" upon echoing, like:
$now = new DateTime; $monday = clone $now;$sunday = clone $now;for ($day = clone $monday; $day < $sunday; $day->modify('+1 day')) {echo $day->format('Y-m-d');}

But that's shouldn't even enter the loop once, since $day will always be equal to $sunday rather than less than it. You'd have to modify $sunday, so that it's later than $monday, like:

$now = new DateTime; $monday = clone $now;$sunday = clone $now;$sunday->modify('+7 days'); for ($day = clone $monday; $day < $sunday; $day->modify('+1 day')) {echo $day->format('Y-m-d');}

At last, now it works,thanks. :happy0046:Now i want to see sth in Dsonesuk's code.
Link to comment
Share on other sites

$now could be tuesday date, wednes.....etc therefore so will $monday, $sunday you have to define the actual monday and sunday values to get the range to loop through
<?php$now = new DateTime;//$now->modify("+1 day");//$now->modify("+2 days");//$now->modify("+3 days");//$now->modify("+4 days");//$now->modify("+5 days");//$now->modify("+6 days");//$now->modify("+7 days"); $monday = clone $now;$monday->modify("+1 day")->modify("last Monday"); $sunday = clone $now;$sunday->modify("-1 day")->modify("next Sunday"); for ($day = clone $monday; $day <= $sunday; $day->modify('+1 day')) {echo $day->format('Y-m-d').'<br />';}?>

the +1 and -1 days are used if on Monday, the last monday would the Monday week before, by going forward 1 day and selecting last Monday it becomes current last Monday, and then do reverse for Sunday.

Yes you right to say that but i have already taken that into consideration. Look below and tell me what you think:
$now = new DateTime($newdate);$week = $now->format('l') === 'Sunday' ? 'previous' : 'this';$monday = clone $now;$monday->modify('Monday ' . $week . ' week');$now->format('j');$sunday1=clone $now;$sunday1->modify('+7 days');$sunday = clone $now;$sunday->modify('Sunday ' . $week . ' week');

SO, with the above code i make sure that "monday" DOES refer to monday and not "now", meaning today's date.

Link to comment
Share on other sites

Yes you right to say that but i have already taken that into consideration. Look below and tell me what you think:
$now = new DateTime($newdate);$week = $now->format('l') === 'Sunday' ? 'previous' : 'this';$monday = clone $now;$monday->modify('Monday ' . $week . ' week');$now->format('j');$sunday1=clone $now;$sunday1->modify('+7 days');$sunday = clone $now;$sunday->modify('Sunday ' . $week . ' week');

SO, with the above code i make sure that "monday" DOES refer to monday and not "now", meaning today's date.

That works as well, but i gather $sunday1 is to do with another part of code somewhere else?
Link to comment
Share on other sites

That works as well, but i gather $sunday1 is to do with another part of code somewhere else?
What do you mean? Can you be more specific please?
Link to comment
Share on other sites

$newdate = '30-04-2012';//$newdate = '02-05-2012';$now = new DateTime($newdate);$week = $now->format('l') === 'Sunday' ? 'previous' : 'this';$monday = clone $now;$monday->modify('Monday ' . $week . ' week'); /*$now->format('j');$sunday1=clone $now;$sunday1->modify('+7 days');	 not required here */$sunday = clone $now;$sunday->modify('Sunday ' . $week . ' week');for ($day = clone $monday; $day <= $sunday; $day->modify('+1 day')) {echo $day->format('Y-m-d').'<br />';}

Link to comment
Share on other sites

You don't need to loop through current weekdays, just identify if current day falls between start - end dates, and apply id to tr element
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title> <style type="text/css"> .mytable td.weekdays {background-color:#FFFF99;}.mytable td.weekend {background-color: #FFCC99;}  .mytable #current_week td.weekdays, .mytable #current_week td.weekend  {background-color:#FF0033;} </style>  </head><body><?php$current = new DateTime;$monday   = new DateTime;$sunday   = new DateTime; $current = $current->format('Ymd'); // change from today to //$current = $current->modify("-1 week")->format('Ymd'); // last week $startDate = $monday->modify("tomorrow")->modify("last Monday")->format('Ymd');$endDate =   $sunday->modify("yesterday")->modify("next Sunday")->format('Ymd'); echo 'Monday = '.$startDate.'<br />';echo 'Sunday = '.$endDate.'<br />';echo 'current = '.$current.'<br />';?><table class="mytable"><tr><td class="weekdays">23</td><td class="weekdays">24</td><td class="weekdays">25</td><td class="weekdays">26</td><td class="weekdays">27</td><td class="weekend">28</td><td class="weekend">29</td></tr><?php if($current >= $startDate && $current <= $endDate){echo '<tr id="current_week">';}else{echo '<tr>';} ?><td class="weekdays">30</td><td class="weekdays">1</td><td class="weekdays">2</td><td class="weekdays">3</td><td class="weekdays">4</td><td class="weekend">5</td><td class="weekend">6</td></tr></table></body></html>

Yes,that approach seems better,instead of looping through dates.There is one code segment i do not understand though.
$startDate = $monday->modify("tomorrow")->modify("last Monday")->format('Ymd');$endDate =   $sunday->modify("yesterday")->modify("next Sunday")->format('Ymd');

Why you want to use 2 modify methods,the one next to each other?Why not for example not use modify->(this monday), modify->(this sunday)? I miss something here.

Link to comment
Share on other sites

Because, $monday= setdate, if this day is on Sunday, goes FORWARD 1 day, now you are in Monday, therefore 'this Monday' would target that Monday (next weeks Monday), whereas 'last Monday' brings you back the 'Monday' of the current week you are working to. Hope that makes sense! unless you mean? why not$startDate = $monday->modify("tomorrow");$startDate = $startDate->modify("last Monday");$startDate = $startDate->format('Ymd');$endDate = $sunday->modify("yesterday");$endDate = $endDate->modify("next Sunday");$endDate = $endDate->format('Ymd');instead$startDate = $monday->modify("tomorrow")->modify("last Monday")->format('Ymd');$endDate = $sunday->modify("yesterday")->modify("next Sunday")->format('Ymd');lot shorter

Edited by dsonesuk
Link to comment
Share on other sites

.... instead$startDate = $monday->modify("tomorrow")->modify("last Monday")->format('Ymd');$endDate = $sunday->modify("yesterday")->modify("next Sunday")->format('Ymd');lot shorter
I use your code and something is wrong-the tr element is not highlighted as expected,i think your code is OK, probably my syntax is somewhere wrong,Look at the code-it is short:
$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');if(isset($_GET['month']))							   {echo  '<tr class="monthgreen" >';}							 elseif								 (isset($_GET['weekview']))								    {  								   if($today>= $startDate && $today <= $endDate)								    {echo   '<tr id="current_week" >';}								   else									   {echo '<tr class="john">';}									 }									 								   else								    echo '<tr class="john">';

Instead of $current I used $now, it is better for me, for clarity reason. That is one thing.The other is the echo statements above where i got to implement the conditional you gave me. Do not be distracted from "monthgreen", this is for when the users are seeing the month-this is OK. What do you think? If there is a syntax error i cannot find it.

Link to comment
Share on other sites

to be clear, a syntax error is when you have an error in the code itself, like missing a semi-colon, or not matching a closing curly brace to an open one. When your code works, but does't produce the desired output, and there are no syntax errors, then you what is called a logic error. Which basically means your implementation is off. Syntax errors are easy to resolve by turning error reporting. Logic errors require debugging to test values and conditionals to see what the code is doing, versus what you think it's doing.

Link to comment
Share on other sites

You are correct. So, I am having a logic error. I have a lead and i will follow it-see where i will get.

Edited by jimfog
Link to comment
Share on other sites

what is $monday referring to? if $now is the date you are checking against, surly you would want to check against the monday (start date) and sunday (end date) that it falls between.

$now = new DateTime($newdate);$today=$now->format('Ymd'); $monday = clone $now;$sunday = clone $now;$startDate = $monday->modify("tomorrow")->modify("last Monday")->format('Ymd');$endDate =  $sunday->modify("yesterday")->modify("next Sunday")->format('Ymd');

EDIT: its the way i formatted the dates ('Ymd') produces 20120503 which is a better value to do calculations against, than ('j m Y') 3 05 2012 or ('Ymj') 2012053 as theres no leading zeroes for last digitYmjMonday = 20120430 = 20,120,430Sunday = 2012056 = 2,012,056current = 2012053 = 2,012,053 compared toYmdMonday = 20120430 = 20,120,430Sunday = 20120506 = 20,120,506current = 20120503 = 20,120,503

Edited by dsonesuk
Link to comment
Share on other sites

Ok now it works,thanks for that but the problem is that all the rows of the calendar gets id="current_week", now we are talking abouthtml problem and not PHP, php works. The code is the one you gave me here http://w3schools.invisionzone.com/index.php?showtopic=42239&hl=&fromsearch=1The only change i made is to make tr class change when the user on the day or month, about week, we are still trying-but i think we are closeIn short here it is:

if ($colcout == 1) {														 if(isset($_GET['month']))							   {echo  '<tr class="monthgreen" >';}							 elseif								 (isset($_GET['weekview']))								    {  								   if($today>= $startDate && $today <= $endDate)								    {echo   '<tr id="current_week" >';}								   else									   {echo '<tr class="jn">';}									 }									  else								    echo '<tr class="john">';									 }									

The problem is depicted in this picture here, which is taken from firebug:https://skydrive.live.com/redir.aspx?cid=be27434b2aac8130&resid=BE27434B2AAC8130!231&parid=BE27434B2AAC8130!140 Thanks again

Link to comment
Share on other sites

i'm not sure how it could be an HTML problem if the PHP is what's generating the HTML.
Then I should say better "HTML", i am putting it in quotes-since as you said PHP generates the HTML here.
Link to comment
Share on other sites

Ok now it works,thanks for that but the problem is that all the rows of the calendar gets id="current_week", now we are talking abouthtml problem and not PHP, php works.
Err, sorry i have to disagree, its the php that assigns the id ref depending on the result of the if condition. try adding $today, $endDate to temp class to identify what it is checking against {echo '<tr id="current_week" class="td_'.$today.' sd_'.$startDate.' ed_'.$endDate.'">';}$today can't be a static date, it must the date applied within the td cells themselves, because yes, it will show repeated id ref , because yes the static date IS always between start and end dates.
Link to comment
Share on other sites

It doesn't seem logical that today should fall in range does it? Today is only one day. It should only be matched once within whatever range you are checking against.

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
 Share

×
×
  • Create New...