note 68716 added to function.date
| From: | PhilSylvia at osu1 dot php dot net | Date: | Tue, 08 Aug 2006 16:05:43 +0000 |
| Subject: | note 68716 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115659@lists.php.net to get a copy of this message | ||
I simplified this after I figured it out based upon Mel Boyce's simple solution. Thanks Mel!
I wanted to calculate dates based upon any given date and not just todays date which is what the
hundreds of examples on the Internet use. I created a simple function and then just call the
function with 2 parameters.. the date (string) to test and the number of days that I want to add
(positive #) or subtract (negative #) My intended use is to retrieve dates from the database and
perform the date calculations. This makes it simple. I hope this helps someone as frustrated as I
was. Enjoy.
******************************************
<?php
// date calculation function
// adds or subtracts a date based upon the input.
// $this_date is a string format of a valid date ie.. "2006/08/11"
// $num_days is the number of days that you would like to add (positive number) or subtract
(negative number)
function fnc_date_calc($this_date,$num_days){
$my_time = strtotime ($this_date); //converts date string to UNIX timestamp
$timestamp = $my_time + ($num_days * 86400); //calculates # of days passed ($num_days) * # seconds
in a day (86400)
$return_date = date("Y/m/d",$timestamp); //puts the UNIX timestamp back into string
format
return $return_date;//exit function and return string
}//end of function
$date_to_test = "2006/08/11";
$days_to_add = 7;
$past_date = fnc_date_calc($date_to_test,(($days_to_add)*-1));
$future_date = fnc_date_calc($date_to_test,$days_to_add);
echo "Test Date is: ".$date_to_test;
echo "<br>";
echo "Number of days to Calculate is: ".$days_to_add;
echo "<br>";
echo "Past date is: ".$past_date;
echo "<br>";
echo "Future date is: ".$future_date;
?>
----
Server IP: 64.71.164.2
Probable Submitter: 70.121.148.37
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/68716
Del: integrated -- https://master.php.net/note/delete/68716/integrated
Del: useless -- https://master.php.net/note/delete/68716/useless
Del: bad code -- https://master.php.net/note/delete/68716/bad+code
Del: spam -- https://master.php.net/note/delete/68716/spam
Del: non-english -- https://master.php.net/note/delete/68716/non-english
Del: in docs -- https://master.php.net/note/delete/68716/in+docs
Del: other reasons-- https://master.php.net/note/delete/68716
Reject -- https://master.php.net/note/reject/68716
Search -- https://master.php.net/manage/user-notes.php