note 56257 added to function.date
| From: | trucexatgmail at osu1 dot php dot net | Date: | Sun, 28 Aug 2005 22:24:53 +0000 |
| Subject: | note 56257 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94316@lists.php.net to get a copy of this message | ||
In response to aedude (gordon from freepgs.net):
I have modified your function so it makes more sense and is more useful.
<?php
function datediff($date1, $date2) {
// $date1 is subtracted from $date2.
// if $date2 is not specified, then current date is assumed.
//Splits date apart
list($date1_month, $date1_day, $date1_year) = split('[/.-]', $date1);
if (!$date2) {
$date2_year = date("Y"); //Gets Current Year
$date2_month = date("m"); //Gets Current Month
$date2_day = date("d"); //Gets Current Day
} else {
list($date2_month, $date2_day, $date2_year) = split('[/.-]', $date2);
}
$date1 = mktime(0,0,0,$date1_month, $date1_day, $date1_year); //Gets Unix timestamp for $date1
$date2 = mktime(0,0,0,$date2_month, $date2_day, $date2_year); //Gets Unix timestamp for $date2
$difference = $date2-$date1; //Calcuates Difference
return floor($difference/60/60/24); //Calculates Days Old
}
?>
New Usage:
<?
//Get how many days since a file has been modified:
echo "$file was modified " . datediff($filemtime($file), 0) . " days ago.";
//Outputs:
// Somefile.txt was modified 6 days ago.
?>
Or
<?
$sally_bday = "11-10-87";
$frank_bday = "11-05-86";
echo "Frank is " . datediff($frank_bday, $sally_bday) . "days older than
Sally.";
//Outputs:
//Frank is 370 days older than Sally.
?>
As you can see, either example can be expanded upon greatly. Keep in mind this function does not do
well with leap years or more precise time as-is, though it could be modified to do so if needed. A
more practical use for this is to do something like delete files older than 30 days from a server,
or see which file/value is older.
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56257
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56257&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56257&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56257&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56257&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56257&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56257&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56257&report=yes
Search -- http://master.php.net/manage/user-notes.php