note 26596 added to function.date
| From: | arial at projectdeath dot com | Date: | Wed, 06 Nov 2002 09:13:14 +0000 |
| Subject: | note 26596 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-39027@lists.php.net to get a copy of this message | ||
Here's a more complex code I have put together from "wtfo at technocraft dot
com"'s source he/she submited. This will tell you the difference between 2 dates in the
following formats:
[X]yrs [X]mos [X]wks [X]days
[X]mos [X]wks [X]days
[X]wks [X]days [X]hrs
[X]days [X]hrs [X]mins
[X]hrs [X]mins [X]secs
[X]mins [X]secs
[X]secs
Depending on the difference between the two times.
function parsefortime ($u) {
//Strip out letters
$u=ereg_replace("[a-zA-z]","",$u);
if (ereg("[^0-9]",$u)) {
$result = 0;
//Use whatever delimeter is there.
$u=split("[^0-9]",$u);
$result += array_pop($u) * 1; //Seconds
$result += array_pop($u) * 60; //Minutes
$result += array_pop($u) * 3600; //Hours
$result += array_pop($u) * 86400; //Days
$result += array_pop($u) * 604800; //Weeks
$result += array_pop($u) * 2592000; //Months
$result += array_pop($u) * 31536000; //Years
} else {
$years = floor($u / 31536000);
$u -= $years * 31536000;
$months = floor($u / 2592000);
$u -= $months * 2592000;
$weeks = floor($u / 604800);
$u -= $weeks * 604800;
$days = floor($u / 86400);
$u -= $days * 86400;
$hours = floor($u / 3600);
$u -= $hours * 3600;
$minutes = floor($u / 60);
$u -= $minutes * 60;
$seconds = $u;
if ($hours=="1") {
$nhour = "hr ";
} elseif ($hours=="0") {
$nhour = "";
$hours = "";
} else {
$nhour = "hrs ";
}
if ($minutes=="1") {
$nminute = "min ";
} elseif ($minutes=="0") {
$nminute = "";
$minutes = "";
} else {
$nminute = "mins ";
}
if ($seconds=="1") {
$nsecond = "sec ";
} elseif ($seconds=="0") {
$nsecond = "";
$seconds = "";
} else {
$nsecond = "secs ";
}
if ($years=="1") {
$nyear = "yr ";
$nminute = "";
$minutes = "";
$seconds = "";
$nsecond = "";
$hours = "";
$nhour = "";
} elseif ($years=="0") {
$nyear = "";
$years = "";
} else {
$nyear = "yrs ";
$nminute = "";
$minutes = "";
$seconds = "";
$nsecond = "";
$hours = "";
$nhour = "";
}
if ($months=="1") {
$nmonth = "mo ";
$nminute = "";
$minutes = "";
$seconds = "";
$nsecond = "";
} elseif ($months=="0") {
$nmonth = "";
$months = "";
} else {
$nmonth = "mos ";
$nminute = "";
$minutes = "";
$seconds = "";
$nsecond = "";
}
if ($weeks=="1") {
$nweek = "wk ";
$nminute = "";
$minutes = "";
$seconds = "";
$nsecond = "";
} elseif ($weeks=="0") {
$nweek = "";
$weeks = "";
} else {
$nweek = "wks ";
$nminute = "";
$minutes = "";
$seconds = "";
$nsecond = "";
}
if ($days=="1") {
$nday = "day ";
$seconds = "";
$nsecond = "";
} elseif ($days=="0") {
$nday = "";
$days = "";
} else {
$nday = "days ";
$seconds = "";
$nsecond = "";
}
$result = $years.
$nyear.
$months.
$nmonth.
$weeks.
$nweek.
$days.
$nday.
$hours.
$nhour.
$minutes.
$nminute.
$seconds.
$nsecond;
}
return $result;
}
Example #1 find out how old a date is:
<?
$ndate = "11/4/2002 8:00:00 AM";
$now = date("U");
$then = strtotime("$ndate");
$timeval = $now-$then;
echo parsefortime($timeval);
?>
Example #2 find out how old a file on your server is:
<?
$filemod = filemtime("/path/to/file");
$filemodtime = date("U", $filemod);
$now = date("U");
$compare = $now-$filemodtime;
$timeval = parsefortime($compare);
echo "Filename.file is $timeval old!";
?>
- Arial
--
http://www.php.net/manual/en/function.date.php
http://master.php.net/manage/user-notes.php?action=edit+26596
http://master.php.net/manage/user-notes.php?action=delete+26596
http://master.php.net/manage/user-notes.php?action=reject+26596