Jump to content

more efficient code


jimfog

Recommended Posts

Take a look at the following code,the date function is assigned twice in the GET array, with different array name element each:

if (!isset($_GET["month"])) $_GET["month"] = date("n");        if (!isset($_GET["monthcol"])) $_GET["monthcol"] = date("n");    

Is there a way can achieve the same result by using more efficient coding?

Link to comment
Share on other sites

Take a look at the following code,the date function is assigned twice in the GET array, with different array name element each:
if (!isset($_GET["month"])) $_GET["month"] = date("n");		if (!isset($_GET["monthcol"])) $_GET["monthcol"] = date("n");	

Is there a way can achieve the same result by using more efficient coding?

you can probably just assign the date("n") value before using it, just you don't at least repeat yourself. I don't know if this is clearer or cleaner, but it's an option
$dateN = $date("n");  $month = (isset($_GET["month"])) ? $_GET["month"] : $dateN;$monthCol = (isset($_GET["monthcol"])) ? $_GET["monthcol"] : $dateN;

I would get in the habit of assigning query parameters to a local variable rather than overwriting the value in the global array. easier to write and work with, IMO.

Link to comment
Share on other sites

The code you present, it is an option, but not more efficient. The same amount of coding is required as my version. Maybe that the best that can be done.

I would get in the habit of assigning query parameters to a local variable rather than overwriting the value in the global array. easier to write and work with, IMO.
I have done that,i just did not included above, here it is:
$cMonth = $_GET["month"]; $cMonthcol=$_GET["monthcol"];

The code you see here is continuation of the code presented in the beginning of the topic. For your info, $cMonth and $cMonth are "echoed" somewhere in the page, outputting the date to the browser.

Link to comment
Share on other sites

If by efficient, you mean that it runs better. It's more efficient to call date('n') once and assign it to a variable than to call it twice. If you want to write less code, there's not that much you can do.

Link to comment
Share on other sites

The code you present, it is an option, but not more efficient. The same amount of coding is required as my version. Maybe that the best that can be done.
as per Ingolme's comment, my version is about as optimized as you can get, but there's very little optimization/efficiency tweaking you can do when all you are doing is variable assignment statements.
Link to comment
Share on other sites

It's more efficient to call date('n') once and assign it to a variable than to call it twice.
That will be the ideal but is difficult to follow that route for the following reasons. Imagine you have a webpage, in the left you have a date, such as < February 2012> and in the right, 9 February 2012, much like outlook. I use one link which sends data to GET to update the "left date" and one link for the "right date". For example the by clicking the right link(the "previous" button for example) we get 8 February 2012. So, is it possible to use just one variable(which has the date function passed to it) to use it in both the above links? I have tried it and the problem is that(of course) both dates change, "left" and "right"(sth i do not want),-by clicking one of the links(left or right)-since there is only one variable corresponding to it. What do you think? I wonder if the solutions is to use Classes.
Link to comment
Share on other sites

I see you intend to make a calendar. As long as you're using the exact same value more than once, assign it to a variable. If you're changing the date you want, you will need to keep calling the date() function to get the new values. You can pass a Unix Timestamp as a second parameter to the date() function.

Link to comment
Share on other sites

Instead of passing individual day, month, year pass the the whole date instead, then you can use date() with appropriate format to show specific part of the of the date, also you can use this one value to access the events also;

<!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></head><body><?php $newdate = date("d-m-Y"); getNewDate($newdate);  if(isset($_GET['prev'])){$newdate = $_GET['prev'];getNewDate($newdate);} if(isset($_GET['next'])){$newdate = $_GET['next'];getNewDate($newdate);} function getNewDate($arg){global $nextDate, $prevDate;$prevDayCalc = strtotime ( '-1 day' , strtotime ( $arg ) ) ;$prevDate= date("d-m-Y", $prevDayCalc); $nextDayCalc = strtotime ( '+1 day' , strtotime ( $arg ) ) ;$nextDate= date("d-m-Y", $nextDayCalc); }  ?><table width="800" border="1" cellspacing="0" cellpadding="0">  <tr>	<td><a href="datephp.php?prev=<?php echo $prevDate ?>">prev</a></td>	<td>echo $newdate.' Or by '.date("d", strtotime ($newdate)).' Or by '.date("m", strtotime ($newdate)).' Or even by '.date("Y", strtotime ($newdate)); ?></td>	<td><a href="datephp.php?next=<?php echo $nextDate ?>">next</a></td>  </tr></table> </body></html>

Edit: realized you have to change both next, prev date on click of any link, so i grouped it all into a function

Link to comment
Share on other sites

Let me test/check the code thoroughly and get back and report.

Link to comment
Share on other sites

shrunk down even more

<!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> <script type="text/javascript">/*<![CDATA[*//*---->*/ /*--*//*]]>*/</script>  <style type="text/css"></style>  </head><body><?php $newdate = date("d-m-Y"); setPrevNextDates($newdate); if(isset($_GET['newdate'])){$newdate = $_GET['newdate'];setPrevNextDates($newdate);} function setPrevNextDates($arg){global $nextDay, $prevDay, $nextMonth, $prevMonth, $nextYear, $prevYear;$prevDayCalc = strtotime ( '-1 day' , strtotime ( $arg ) ) ;$prevDay= date("d-m-Y", $prevDayCalc); $nextDayCalc = strtotime ( '+1 day' , strtotime ( $arg ) ) ;$nextDay= date("d-m-Y", $nextDayCalc); $prevMonCalc = strtotime ( '-1 month' , strtotime ( $arg ) ) ;$prevMonth= date("d-m-Y", $prevMonCalc); $nextMonCalc = strtotime ( '+1 month' , strtotime ( $arg ) ) ;$nextMonth= date("d-m-Y", $nextMonCalc); $prevYearCalc = strtotime ( '-1 year' , strtotime ( $arg ) ) ;$prevYear= date("d-m-Y", $prevYearCalc); $nextYearCalc = strtotime ( '+1 year' , strtotime ( $arg ) ) ;$nextYear= date("d-m-Y", $nextYearCalc);} ?><table width="800" border="1" cellspacing="0" cellpadding="0"><tr><td colspan="3"></td><td align="center"><?php echo date("F", strtotime ($newdate)).' '.date("Y", strtotime ($newdate)); ?></td><td colspan="3"></td></tr>  <tr><td><a href="datephp.php?newdate=<?php echo $prevYear ?>">prev Year</a></td>  <td><a href="datephp.php?newdate=<?php echo $prevMonth ?>">prev month</a></td>	<td><a href="datephp.php?newdate=<?php echo $prevDay ?>">prev day</a></td>	<td><?php echo $newdate.' Or by '.date("d", strtotime ($newdate)).' Or by '.date("F", strtotime ($newdate)).' Or even by '.date("Y", strtotime ($newdate)); ?></td>	<td><a href="datephp.php?newdate=<?php echo $nextDay ?>">next day</a></td>	<td><a href="datephp.php?newdate=<?php echo $nextMonth ?>">next month</a></td>	<td><a href="datephp.php?newdate=<?php echo $nextYear ?>">next Year</a></td>  </tr></table> </body></html>

Edit: but now a bit extra as i added month, and year updates

Link to comment
Share on other sites

Dsonesuk, One question, in both the version above you provide functions to calculate the previous or next day. Is this supposed to be "printed" somewhere in the browser or you just wrote it for demonstration? Clinking the links, does increase/decrease the day(which is reflected in the url) but after that i get an object not found page. Am i doing sth wrong or this is the behavior that ought to be from the beginning. This is my version of the code-tell me the negatives, if any:

   if (!isset($_GET["month"])) $_GET["month"] = date("n");	 if (!isset($_GET["year"]))  $_GET["year"]  = date("Y");$cMonth = $_GET["month"];////Now the month is 1-meaning january  $cYear  = $_GET["year"];$next_month = $cMonth+1;  $prev_month = $cMonth-1; echo '<a href="'.$_SERVER["PHP_SELF"]. "?month= ". $prev_month ." &year= " .$prev_year. '">		    <img src="Images/prevmont_arrow.png" width="12" height="14" alt="προηγούμενος μήνας" /></a>'; 		  		   echo  '<div class="monthname">'.$monthNames[$cMonth - 1].' '. $cYear.'</div>';			 		    echo   '<a href="'.$_SERVER["PHP_SELF"] . "?month=".$next_month.'&year='.$next_year.'">		   <img src="Images/nextmont_arrow.png" width="12" height="14"  alt="επόμενος μήνας" /></a>';

Link to comment
Share on other sites

All prevday, prevmonth hold the whole date ref as if you go by 10-02-2012 the prevday will be 09-02-2012, the prevmonth will be 10-01-2012 and so on, to gain access to the day, month year only you would convert it to time stamp by using strtotime function, as in date("d", strtotime($prevday)) (same as to retrieve the day of 09 as if you where using date("d", strtotime('09-02-2012')), i'm not going by the day, or month values on there own. see example below

<!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><script type="text/javascript">/*<![CDATA[*//*---->*//*--*//*]]>*/</script><style type="text/css">table {margin: 20px auto;}table td {text-align:center;}</style></head><body><?php$newdate = date("d-m-Y");$leapYear=date("L");setPrevNextDates($newdate);if(isset($_GET['newdate'])){$newdate = $_GET['newdate'];setPrevNextDates($newdate);}function setPrevNextDates($arg){global $nextDay, $prevDay, $nextMonth, $prevMonth, $nextYear, $prevYear, $leapYear ;$prevDayCalc = strtotime ( '-1 day' , strtotime ( $arg ) ) ;$prevDay= date("d-m-Y", $prevDayCalc);$nextDayCalc = strtotime ( '+1 day' , strtotime ( $arg ) ) ;$nextDay= date("d-m-Y", $nextDayCalc);$prevMonCalc = strtotime ( '-1 month' , strtotime ( $arg ) ) ;$prevMonth= date("d-m-Y", $prevMonCalc);$nextMonCalc = strtotime ( '+1 month' , strtotime ( $arg ) ) ;$nextMonth= date("d-m-Y", $nextMonCalc);$prevYearCalc = strtotime ( '-1 year' , strtotime ( $arg ) ) ;$prevYear= date("d-m-Y", $prevYearCalc);$nextYearCalc = strtotime ( '+1 year' , strtotime ( $arg ) ) ;$nextYear= date("d-m-Y", $nextYearCalc);$LeapYearCalc=strtotime( $arg );$leapYear = date("L", $LeapYearCalc);if($leapYear==1 && date("m", strtotime ($arg)) == 2 && date("d",strtotime ($arg)) == 29){$prevYearCalc = strtotime ( '-1 day' , strtotime ( $prevYear ) ) ;$prevYear= date("d-m-Y", $prevYearCalc);$nextYearCalc = strtotime ( '-1 day' , strtotime ( $nextYear ) ) ;$nextYear= date("d-m-Y", $nextYearCalc);}}echo $leapYear.' 1 equals leap year';?><table width="960" border="1" cellspacing="0" cellpadding="0"><tr>  <td>code:</td>  <td>date("Y", strtotime($prevYear));</td>  <td>date("m", strtotime($prevMonth));</td>  <td>date("d", strtotime($prevDay));;</td>  <td align="center"> </td>  <td>date("d", strtotime($nextDay));</td>  <td>date("m", strtotime($nextMonth));</td>  <td>date("Y", strtotime($nextYear));</td></tr><tr>  <td>covert string val into time stamp and get specific date value</td>  <td><?php echo date("Y", strtotime($prevYear)); ?></td>  <td><?php echo date("m", strtotime($prevMonth)); ?></td>  <td><?php echo date("d", strtotime($prevDay)); ?></td>  <td align="center"> </td>  <td><?php echo date("d", strtotime($nextDay)); ?></td>  <td><?php echo date("m", strtotime($nextMonth)); ?></td>  <td><?php echo date("Y", strtotime($nextYear)); ?></td></tr><tr>  <td>textual ref of dates</td>  <td><?php echo $prevYear ?></td><td><?php echo $prevMonth ?></td><td><?php echo $prevDay ?></td><td align="center"><?php echo date("F", strtotime ($newdate)).' '.date("Y", strtotime ($newdate)); ?></td><td><?php echo $nextDay ?></td><td><?php echo $nextMonth ?></td><td><?php echo $nextYear ?></td></tr>  <tr>    <td> </td>    <td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?newdate=<?php echo $prevYear ?>">prev Year</a></td>  <td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?newdate=<?php echo $prevMonth ?>">prev month</a></td>    <td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?newdate=<?php echo $prevDay ?>">prev day</a></td>    <td align="center"><?php echo date("d", strtotime ($newdate)).' - '.date("F", strtotime ($newdate)).' - '.date("Y", strtotime ($newdate)); ?></td>    <td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?newdate=<?php echo $nextDay ?>">next day</a></td>    <td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?newdate=<?php echo $nextMonth ?>">next month</a></td>    <td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?newdate=<?php echo $nextYear ?>">next Year</a></td>  </tr></table></body></html>

everytime any of the links are click all change update instantly, and i also recieved no object error with my examples.

Link to comment
Share on other sites

Now that code is WORKING-let me study it and get back IF i have a problem again. MANY THANKS.

Link to comment
Share on other sites

I am looking at this line here:

 <td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?newdate=<?php echo $prevDay ?>">prev day</a></td>

Does the $prevDay variable contain 13-02-2012 in it? Am i "reading" it correctly? I have never come across before in PHP with the concept of date textual reference. And another thing, since the above code will send the prevday value to the GET array through the URL, is it necessary to use echo in it? Unless this also the only way to output value from a variable to the URL. And one last question, why do you want to convert the textual reference(using strtotime) to a UNIX timestamp.Does this help in making the calculations(prevday, prevmonth...etc)?

Link to comment
Share on other sites

Does the $prevDay variable contain 13-02-2012 in it? Am i "reading" it correctly?
Yes! the $prevDay value will always show the previous day value of whatever the current date ($newdate) in the centre column is , $newdate = 14-02-2012, so $prevDay will equal 13-02-2012. The centre column is using <?php echo date("d", strtotime ($newdate)).' - '.date("F", strtotime ($newdate)).' - '.date("Y", strtotime ($newdate)); ?> to show the date as '14 - February - 2012'
And another thing, since the above code will send the prevday value to the GET array through the URL, is it necessary to use echo in it?
you have to echo the variable to show its value in the GET querystring value either by
<td><a href="<?php echo $_SERVER["PHP_SELF"]; ?>?newdate=<?php $prevDay ?>">prev day</a></td>

OR

 echo '<td><a href="'.$_SERVER["PHP_SELF"].'?newdate='.$prevDay.'">prev day</a></td>';

Link to comment
Share on other sites

Ok, and sth else,in the previous version of the code i had created an array where the months of the year were contained in Greek and when the month was changing, the name of it was taken from that array:

$monthNames = Array("Γενάρης", "Φλεβάρης", "Μάρτιος", "Απρίλιος", "Μάης", "Ιούνιος", "Ιούλιος", "Αύγουστος", "Σεπτέμβριος",	  "Οκτωβριος", "Νοέμβριος", "Δεκέμβριος"); $monthNames1[$cMonthcol- 1].

Above, in the array you will find the names of the month-in Greek. What should i do now so that the months appear in Greek, access the array "somehow" or use locale php directives?

Link to comment
Share on other sites

Ok, i solved the issue with the names of the month.One issue that troubles me though is the following-it is not a problem just i want to understand the code as good as possible: I see that you call setPrevNextDates($newdate) first:

setPrevNextDates($newdate);

Why do you do that?I see of course that if i remove the above line prev,next calculations will not work anymore, but i do not understand why this happens. Since, the above function is called anyway when a link is clicked and data gets sent in the GET array:

if (isset($_GET['newdate'])) {	 $newdate = $_GET['newdate'];	 setPrevNextDates($newdate);}

Thanks again for the help, I think i got it, you call the function first so as to set it from before in today's date.Am i correct?

Link to comment
Share on other sites

all the previ/next are setup to be processed once a link is clicked to identify the date to work to usingif (isset($_GET['newdate'])) So on initial startup of page this will be bypassed, so it needs a date to produce all the prev/next dates this is where $newdate = date("d-m-Y"); comes into play it gathers todays date and then setPrevNextDates($newdate); processes todays prev/next dates for the links, if any of the next/prev links are clicked the if condition will pick this up and apply the value from the selected link overwriting value setup by previously by$newdate = date("d-m-Y"); you could place this in an 'else condition' of the 'if condition' to prevent it calling the function everytime the page is reloaded when any links are clicked, which might be a better option

$leapYear=date("L");if(isset($_GET['newdate'])){$newdate = $_GET['newdate'];setPrevNextDates($newdate);}else{$newdate = date("d-m-Y");setPrevNextDates($newdate);}

Link to comment
Share on other sites

GOT IT...there is another issue also(the biggest), but i am going to try find it myself. If i have a problem i will post again.

Link to comment
Share on other sites

As i said again i am building a calendar that looks like outlook. In outlook there is also a week view, so we must find a way now so that the calendar goes from week to week(previous and next). So far we have done that with month day and year. I tried to accomplish that with the following:

$begweekCalc=strtotime ( '-1 week' , strtotime ( $arg ) ) ;$begweek=date("d-m-Y", $begweekCalc);

But the above logic has a flaw. If for example today we have Tuesday the above code will "transfer" us to the previous Tuesday. What i want though, is, to be transferred to the beginning of the week, namely, Monday. I have some ideas as to how am i going to achieve that but i am interested also to your opinion.

Link to comment
Share on other sites

Well, the logic is sounds OK to me-and easy, nonetheless, the coding will be the difficult part since i have not done it again. I will try though and get back if i hit a dead-end.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...