FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Date within a Timeframe





pashmina
Code:
/**
 * Checks to see if a timestamp was within a timeframe
 * @param int $check
 * @param int $start
 * @param int $finish
 * @return boolean
 */
function withinTimeframe($check, $start, $finish = '') {
   if (!is_int($check))   $check = strtotime($check);
   if (!is_int($start))   $start = strtotime($start);
   
   if (empty($finish)) {
      $finish = time();
   } else {
      if (!is_int($finish)) $finish = strtotime($finish);
   }
   
   if ($check >= $start && $check <= $finish) {
      return true;
   }

   return false;
}


Quote:
Date within a Timeframe
This function will take a date and check to see if it was within a certain date range and return a boolean value. My function will attempt to convert non unix timestamps to unix; be sure to pass the data correctly. Also you can leave the 3rd argument empty ($finish date) and the current time() will be used.


Can somebody teach me how to use this function?

specially about these
* @param int $check
* @param int $start
* @param int $finish
* @return boolean
AftershockVibe
I think that's actually a function written for Java (given the comments) but anyway...

"check" is the timestamp of the date you want to check. The function will return true if "check" is inbetween "start" and "finish" or false otherwise. If you don't specify "finish" then it will default to the current system time.
imagefree
* @param int $check => $check variables is expected to contain integer value. int means integer
* @param int $start
* @param int $finish
* @return boolean => the function returns either true or false.

the above four lines are just documentation of the function for better understanding at bird's eye view.

the function withinTimeframe checks whether the time given in $check variable is within the range of times in $start and $finish variables, so that $check is greater than $start and shorter than $finish.

The function withinTimeframe expects 2 or three values. If you give 2 values to it, it will automatically assume that the third value is '' (ie empty. Empty means the value exists. Null means value doesnt exist.). Remember!!! such behaviour of adding the last value automatically is not php's default behaviour.

If you give 3 values, it does no change to input.

After the function is successfully called using call:

Code:
withinTimeframe('SOME TIME STAMP' , 'SOME TIME STAMP'  , 'SOME TIME STAMP'  )

or something like that, it first checks whether the variables $check and $start contain the integer value or string. If the value is not integer( as cheked by !is_int() , where ! means not), it assumes that the value inside the function is string and further assumes that the value is a Time stamp in the following format
Code:
January 1 1970 00:00:00 GMT

and then the strtotime function attempts to convert the string into Unix Time Stamp similar to the time returned by time() function.


Once done, it checks whether the $finish variable is empty(ie, set to ''). If yes, then it automatically adds the current time to $finish variable.
If $finish is not empty, it checks the data type if $finish and attempts to convert it to integer value if it is STRING (just as it did in case of $start and $check).

Then comes the main purpose of the script. It checks whether the $check variable's value is greater than or equal to $start 's value and at the same time it is lesser than or equal to $finish's value. If so, it returns true (a boolean value as said in documentation), or otherwise returns false.

There is one thing to note. The return false at the end of the script has no condition, ie whenever this line is executed, the function will return false. So, it appears the function will always return false, but this is not the case.
The if condition just above the said instruction is the key. If the if-condition is true it will return true and terminate the function. In case the condition is false, nothing inside if body is executed and the next instruction ie, return false; is followed.

More?[/b]
rockacola
Code:

/**
 * Checks to see if a timestamp was within a timeframe
 * @param int $check
 * @param int $start
 * @param int $finish
 * @return boolean
 */

This is commenting machanism for PhpDoc (http://www.phpdoc.org/), which is standard in organized php project.

It doesn't do anything to the code and you can ignore it and remove it if it confuses you.
pashmina
imagefree wrote:
* @param int $check => $check variables is expected to contain integer value. int means integer
* @param int $start
* @param int $finish
* @return boolean => the function returns either true or false.

the above four lines are just documentation of the function for better understanding at bird's eye view.

the function withinTimeframe checks whether the time given in $check variable is within the range of times in $start and $finish variables, so that $check is greater than $start and shorter than $finish.

The function withinTimeframe expects 2 or three values. If you give 2 values to it, it will automatically assume that the third value is '' (ie empty. Empty means the value exists. Null means value doesnt exist.). Remember!!! such behaviour of adding the last value automatically is not php's default behaviour.

If you give 3 values, it does no change to input.

After the function is successfully called using call:

Code:
withinTimeframe('SOME TIME STAMP' , 'SOME TIME STAMP'  , 'SOME TIME STAMP'  )

or something like that, it first checks whether the variables $check and $start contain the integer value or string. If the value is not integer( as cheked by !is_int() , where ! means not), it assumes that the value inside the function is string and further assumes that the value is a Time stamp in the following format
Code:
January 1 1970 00:00:00 GMT

and then the strtotime function attempts to convert the string into Unix Time Stamp similar to the time returned by time() function.


Once done, it checks whether the $finish variable is empty(ie, set to ''). If yes, then it automatically adds the current time to $finish variable.
If $finish is not empty, it checks the data type if $finish and attempts to convert it to integer value if it is STRING (just as it did in case of $start and $check).

Then comes the main purpose of the script. It checks whether the $check variable's value is greater than or equal to $start 's value and at the same time it is lesser than or equal to $finish's value. If so, it returns true (a boolean value as said in documentation), or otherwise returns false.

There is one thing to note. The return false at the end of the script has no condition, ie whenever this line is executed, the function will return false. So, it appears the function will always return false, but this is not the case.
The if condition just above the said instruction is the key. If the if-condition is true it will return true and terminate the function. In case the condition is false, nothing inside if body is executed and the next instruction ie, return false; is followed.

More?[/b]


Thank you for explanation. It was really helpful.
I just wanted to use this function with the jquery UI datepicker(http://jqueryui.com/demos/datepicker/#date-formats).

I will create a sample page and post if I encounter any problem.
Related topics
Outsourcing advisory group formed
Interview: Derek Liu, Gaia Online Anime Community
10 Reasons to Date a Hockey Player
Installing, Partitioning & Formatting HDDs
FTP - file last modified date
keep your adblocking up to date
Webpage Display Problem
Your Strategies against lazyness?
Does Beauty Come From Within ?
mysql querries and subquerries problem..
Blackberries
Windows date
Date/Time Entries (PHP & MySQL)
Lost all My Google Contacts
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.