PDA

View Full Version : Countup script


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";
}
?>

marek_mar
01-21-2005, 08:54 PM
The simpler way. Smaller probability of errors.

<?
function simple_countup($time /* feel free to use mktime() to generate a timestamp*/, $format /* php's date() function format */, $offset = 0 /* in hours */)
{
if (!(is_int($time) && strlen($time) == 11))
{ // Let's say this is an unneeded feature.
$time = strtotime($time);
}
if(!empty($offset))
{
$offset *= 3600;
}
$diff_date = time() + $offset - $time;
$more_than_year = date('Y', $diff_date) - 1970; // Hack for non negative timestamps.
$years = ($more_than_year >= 0) ? $more_than_year : '';
return $years . ' year(s) ' . date($format, $diff_date);
}
print 'It\'s ' . simple_countup('5 jan 2004 4:50 AM', 'z \d\a\y\(\s\) H \h\o\u\r\(\s\) i \m\i\n\u\t\e\(\s\) s \s\e\c\o\n\d\(\s\)', 2) . ' since 5 jan 2004 4:50 AM';
?>

Diana*
01-21-2005, 09:48 PM
Thank you.
But I need months and weeks. Is it possilbe?

marek_mar
01-21-2005, 09:56 PM
Just change the format. Read about it on the date() (http://www.php.net/date) function page.

Diana*
01-22-2005, 04:07 AM
Just change the format. Read about it on the date() (http://www.php.net/date) function page.
Thank you for the link.
Now I have these pieces, but don't know how to place them together.
I am new in programming and don't seem to figure it out mylsef :confused:
Can you please help?

// -8 hours from GMT:
$now = mktime(date("H")-8, date("i"), date("s"), date("m"), date("d"), date("Y"));
echo date("l dS of F Y H:i:s A",$now);

$nodaysinmonth = Array("January" => 31,
"February" => 28,
"March" => 31,
"April" => 30,
"May" => 31,
"June" => 30,
"July" => 31,
"August" => 31,
"September" => 30,
"October" => 31,
"November" => 30,
"December" => 31);

/* w e e k n u m b e r -------------------------------------- //
weeknumber returns a week number from a given date (>1970, <2030)
Wed, 2003-01-01 is in week 1
Mon, 2003-01-06 is in week 2
Wed, 2003-12-31 is in week 53, next years first week
Be careful, there are years with 53 weeks.
// ------------------------------------------------------------ */

function weeknumber ($y, $m, $d) {
$wn = strftime("%W",mktime(0,0,0,$m,$d,$y));
$wn += 0; # wn might be a string value
$firstdayofyear = getdate(mktime(0,0,0,1,1,$y));
if ($firstdayofyear["wday"] != 1) # if 1/1 is not a Monday, add 1
$wn += 1;
return ($wn);
} # function weeknumber

/* d a t e f r o m w e e k ---------------------------------- //
From a weeknumber, calculates the corresponding date
Input: Year, weeknumber and day offset
Output: Exact date in an associative (named) array
2003, 12, 0: 2003-03-17 (a Monday)
1995, 53, 2: 1995-12-xx
...
// ------------------------------------------------------------ */

function datefromweek ($y, $w, $o) {

$days = ($w - 1) * 7 + $o;

$firstdayofyear = getdate(mktime(0,0,0,1,1,$y));
if ($firstdayofyear["wday"] == 0) $firstdayofyear["wday"] += 7;
# in getdate, Sunday is 0 instead of 7
$firstmonday = getdate(mktime(0,0,0,1,1-$firstdayofyear["wday"]+1,$y));
$calcdate = getdate(mktime(0,0,0,$firstmonday["mon"], $firstmonday["mday"]+$days,$firstmonday["year"]));

$date["year"] = $calcdate["year"];
$date["month"] = $calcdate["mon"];
$date["day"] = $calcdate["mday"];

return ($date);
echo datefromweek;
} # function datefromweek

marek_mar
01-22-2005, 11:30 AM
To get the current week numeber just use

print date('W');

To get the number of days in the current month use

print date('t');


As you see the date() function is very powerfull and does most of any needed time calculations for you.

You do not need to do this:

$now = mktime(date("H")-8, date("i"), date("s"), date("m"), date("d"), date("Y"));

you could just:

date('l dS of F Y H:i:s A', time() - 8 * 3600);