Diana*
01-21-2005, 08:03 PM
I have the following countup script that shows years, months, weeks and days passed since the exact date (see below). The HTML code is:
<? include('cup.php'); ?>
<? countup(4, 2004, 3, ww, 16, hh, mm, ss); ?>
The PHP script doesn't show weeks though. And I don't think I specified months correctly.
If anybody can help me with these questions it would be really great!
Thank you in advance!
<?
# PHP Countup
define("OFFSET", 0);
define("YSECS", 365*24*60*60);
define("MOSECS", 365*2*60*60);
define("WSECS", (365*24*60*60)/7);
define("DSECS", 24*60*60);
define("HSECS", 60*60);
define("MSECS", 60);
function countup($detail, $year, $month = 0, $week = 0, $day = 0, $hour = 0, $minute = 0, $second = 0) {
$years = $months = $weeks = $days = $hours = $minutes = $seconds = 0;
$now = mktime() + OFFSET*60*60;
$before = mktime($hour, $minute, $second, $month, $day, $year);
$cup = abs($before - $now);
if ($detail == 1) $years = round($cup/YSECS);
else $years = floor($cup/YSECS);
$cup %= YSECS;
if ($detail == 2) $months = round($cup/MOSECS);
else $months = floor($cup/MOSECS);
$cup %= MOSECS;
if ($detail == 3) $weeks = round($cup/WSECS);
else $weeks = floor($cup/WSECS);
$cup %= WSECS;
if ($detail == 4) $days = round($cup/DSECS);
else $days = floor($cup/DSECS);
$cup %= DSECS;
if ($detail == 5) $hours = round($cup/HSECS);
else $hours = floor($cup/HSECS);
$cup %= HSECS;
if ($detail == 6) $minutes = round($cup/MSECS);
else $minutes = floor($cup/MSECS);
$cup %= MSECS;
$seconds = $cup;
$tnums = array($years, $months, $weeks, $days, $hours, $minutes, $seconds);
$ttext = array("year", "month", "week", "day", "hour", "minute", "second");
$shown = 0;
for ($i=0;$i<$detail;$i++) {
if ($tnums[$i]) {
echo "$tnums[$i] $ttext[$i]";
$shown++;
if ($tnums[$i] != 1) echo "s";
$count = 0;
for ($j=$i+1;$j<$detail;$j++) {
if ($tnums[$j]) $count++;
}
switch($count) {
case 0: break 2;
case 1: if ($shown>1) echo ","; echo " and "; break;
default: echo ", "; break;
}
}
}
if ($now < $before) echo " will be";
if ($now == $before) echo "now";
}
?>
<? include('cup.php'); ?>
<? countup(4, 2004, 3, ww, 16, hh, mm, ss); ?>
The PHP script doesn't show weeks though. And I don't think I specified months correctly.
If anybody can help me with these questions it would be really great!
Thank you in advance!
<?
# PHP Countup
define("OFFSET", 0);
define("YSECS", 365*24*60*60);
define("MOSECS", 365*2*60*60);
define("WSECS", (365*24*60*60)/7);
define("DSECS", 24*60*60);
define("HSECS", 60*60);
define("MSECS", 60);
function countup($detail, $year, $month = 0, $week = 0, $day = 0, $hour = 0, $minute = 0, $second = 0) {
$years = $months = $weeks = $days = $hours = $minutes = $seconds = 0;
$now = mktime() + OFFSET*60*60;
$before = mktime($hour, $minute, $second, $month, $day, $year);
$cup = abs($before - $now);
if ($detail == 1) $years = round($cup/YSECS);
else $years = floor($cup/YSECS);
$cup %= YSECS;
if ($detail == 2) $months = round($cup/MOSECS);
else $months = floor($cup/MOSECS);
$cup %= MOSECS;
if ($detail == 3) $weeks = round($cup/WSECS);
else $weeks = floor($cup/WSECS);
$cup %= WSECS;
if ($detail == 4) $days = round($cup/DSECS);
else $days = floor($cup/DSECS);
$cup %= DSECS;
if ($detail == 5) $hours = round($cup/HSECS);
else $hours = floor($cup/HSECS);
$cup %= HSECS;
if ($detail == 6) $minutes = round($cup/MSECS);
else $minutes = floor($cup/MSECS);
$cup %= MSECS;
$seconds = $cup;
$tnums = array($years, $months, $weeks, $days, $hours, $minutes, $seconds);
$ttext = array("year", "month", "week", "day", "hour", "minute", "second");
$shown = 0;
for ($i=0;$i<$detail;$i++) {
if ($tnums[$i]) {
echo "$tnums[$i] $ttext[$i]";
$shown++;
if ($tnums[$i] != 1) echo "s";
$count = 0;
for ($j=$i+1;$j<$detail;$j++) {
if ($tnums[$j]) $count++;
}
switch($count) {
case 0: break 2;
case 1: if ($shown>1) echo ","; echo " and "; break;
default: echo ", "; break;
}
}
}
if ($now < $before) echo " will be";
if ($now == $before) echo "now";
}
?>