Count up timer in PHP
Awhile ago I posted this javascript here that was a count up timer. Many people said if you can do it in PHP why not? So, I wrote the PHP functionality to do the same thing:
<?
// enter start date below like this: “January 2, 2001″
$start = “January 29, 2002“;
// enter string of what this start date is.
$text = “have passed since I had a fulltime job.“;
//————————–
$now = strtotime (”now”);
$then = strtotime (”$start”);
$difference = $now - $then ;
$num = $difference/86400;
$days = intval($num);
$num2 = ($num - $days)*24;
$hours = intval($num2);
$num3 = ($num2 - $hours)*60;
$mins = intval($num3);
$num4 = ($num3 - $mins)*60;
$secs = intval($num4);
?>
<p>
<? echo $days ?> days,
<? echo $hours ?> hours,
<? echo $mins ?> minutes,
<? echo $secs ?> seconds
<? echo $text ?>
</p>
copy and paste the above in your doc - you only need to change the “start” time and text line (noted in red)
Or you can download it here - just follow the instructions in the file
May 24th, 2002 at 2:44 am
Sweet! I’m going to be working on the Resolution Project site over the weekend and I’ll definitely be adding this! (Day 5 of 84 today!)
May 27th, 2002 at 10:38 am
Is there a simple way of just changing a line to make it a count down timer, instead?
Thanks,
Liz
May 27th, 2002 at 10:12 pm
Liz - had to change the code in a few places to do the count down timer. Here it is:
<?
// enter start date below like this: “January 2, 2001″
$end = “September 25, 2002″;
// enter string of what this start date is.
$text = “until my birthday.”;
$textOnTheDay = “Today is my birthday!!!”;
$textAfterwards = “You missed it. My birthday has passed.”;
//-
$now = strtotime (”now”);
$then = strtotime (”$end”);
$difference = $then - $now;
$num = $difference/86400;
$days = intval($num);
$num2 = ($num - $days)*24;
$hours = intval($num2);
$num3 = ($num2 - $hours)*60;
$mins = intval($num3);
$num4 = ($num3 - $mins)*60;
$secs = intval($num4);
if ($days>0 || $hours>0 || $minutes>0 || $seconds>0) {
?>
<p>
<? echo $days ?> days,
<? echo $hours ?> hours,
<? echo $mins ?> minutes,
<? echo $secs ?> seconds
<? echo $text ?>
</p>
<? } else if (($days==0) && ($hours<=0 || $minutes<=0 || $seconds<=0)) { ?>
<p><? echo $textOnTheDay ?><p>
<? } else { ?>
<p> <? echo $textAfterwards ?></p>
<? } ?>
just change whats in red…
May 28th, 2002 at 9:49 am
Woo! Love it!!! This is most certainly very useful.
June 2nd, 2002 at 4:31 am
I still need to add this to the Resolution Project site; I was wondering, is it possible to add a week value before the day value? I was thinking I could figure out how to do it myself, but then I realized I’m not quite that savvy yet! So it would say “2 Weeks, 3 Days, …” on the site. THANKS!
June 2nd, 2002 at 6:08 am
For Count UP timer (with weeks) change the code in the script to this:
//-
$now = strtotime (”now”);
$then = strtotime (”$start”);
$difference = $now - $then ;
$num = $difference/86400;
$num1 = $num/7;
$weeks = intval($num1);
$num1a = $difference/86400 - $weeks*7;
$days = intval($num1a);
$num2 = ($difference/86400 - intval($difference/86400))*24;
$hours = intval($num2);
$num3 = ($num2 - $hours)*60;
$mins = intval($num3);
$num4 = ($num3 - $mins)*60;
$secs = intval($num4);
?>
<p>
<? echo $weeks ?> weeks,
<? echo $days ?> days,
<? echo $hours ?> hours,
<? echo $mins ?> minutes,
<? echo $secs ?> seconds
<? echo $text ?>
</p>
For Count DOWN timer (with weeks) change the code in the script to this:
//-
$now = strtotime (”now”);
$then = strtotime (”$end”);
$difference = $then - $now;
$num = $difference/86400;
$num1 = $num/7;
$weeks = intval($num1);
$num1a = $difference/86400 - $weeks*7;
$days = intval($num1a);
$num2 = ($difference/86400 - intval($difference/86400))*24;
$hours = intval($num2);
$num3 = ($num2 - $hours)*60;
$mins = intval($num3);
$num4 = ($num3 - $mins)*60;
$secs = intval($num4);
if ($weeks>0 || $days>0 || $hours>0 || $minutes>0 || $seconds>0) {
?>
<p>
<? echo $weeks ?> weeks,
<? echo $days ?> days,
<? echo $hours ?> hours,
<? echo $mins ?> minutes,
<? echo $secs ?> seconds
<? echo $text ?>
</p>
<? } else if (($days==0 && $weeks==0) && ($hours<=0 || $minutes<=0 || $seconds<=0)) { ?>
<p><? echo $textOnTheDay ?><p>
<? } else { ?>
<p> <? echo $textAfterwards ?></p>
<? } ?>
June 2nd, 2002 at 6:13 am
Demo and download have been updated.
July 13th, 2002 at 5:31 pm
As always… thank you so much for this! works perfectly!
July 30th, 2002 at 10:02 pm
this rocks, very nice.
any easy way in the countdown timer to format my $now value to be an hour on that given day? say, 3PM on August 1st, 2002?
July 30th, 2002 at 10:13 pm
I *believe* that if you enter your $now time as August 1st, 2002 15:00:00 it may work… you’d have to test it to see… it’s just taking that string and applying “strtotime()” on it. You can read more about that function here
September 1st, 2002 at 7:33 am
hi ive been trying to find a script like this for ages, thanks
however i was wondering if you could get it to do months and years also?
September 14th, 2002 at 12:23 pm
We all know how badly I can mangle scripts *cough* I’m going to try it on my own, with my fingers crossed, but how about one with just weeks & days (or with just days?) Oh, and it’s the countdown … our birthdays are coming up soon! *grin*
March 23rd, 2003 at 10:11 am
I noticed someone wanting to have this script do months, and years, and so after fiddling with it, I have managed to make it do months and years as well. Find the new code below.
<?
$start = “November 22, 1979″;
$text = “since I was born”;
$now = strtotime (”now”);
$then = strtotime (”$start”);
$difference = $now - $then ;
$num = $difference/86400;
$num1 = $num/365;
$years = intval($num1);
$num2 = $num/30;
$months = intval($num2);
$num3 = $num/7;
$weeks = intval($num3);
$num3a = $difference/86400 - $weeks*7;
$days = intval($num3a);
$num4 = ($difference/86400 - intval($difference/86400))*24;
$hours = intval($num4);
$num5 = ($num4 - $hours)*60;
?>
<p>
<? echo $years ?> years,
<? echo $months ?> months,
<? echo $weeks ?> weeks,
<? echo $days ?> days,
<? echo $hours ?> hours,
<? echo $mins ?> minutes,
<? echo $secs ?> seconds
<? echo $text ?>
</p>
March 23rd, 2003 at 10:28 am
Ugh, gotta repost the code. I noticed, after I posted the last comment, that the code was not completly changed, that was an old piece of code.
Ummm, if you can clean the code up, by all means do so, but please email me a copy when you do.
Code follows:
<?
$start = “November 21, 1979″;
$text = “since I was born”;
$now = strtotime (”now”);
$then = strtotime (”$start”);
$difference = $now - $then ;
$num = $difference/86400;
$num1 = $num/365;
$years = intval($num1);
$temp = $difference/86400 - $years*365;
$num2 = $temp/30;
$months = intval($num2);
$temp = $difference/86400 - ($years*365) - ($months*30);
$num3 = $temp/7;
$weeks = intval($num3);
$num3a = $difference/86400 - ($years*365) - ($months*30) - ($weeks*7);
$days = intval($num3a);
$num4 = ($difference/86400 - intval($difference/86400))*24;
$hours = intval($num4);
$num5 = ($num4 - $hours)*60;
$mins = intval($num5);
$num6 = ($num5 - $mins)*60;
$secs = intval($num6);
?>
<p>
<? echo $years ?> years,
<? echo $months ?> months,
<? echo $weeks ?> weeks,
<? echo $days ?> days,
<? echo $hours ?> hours,
<? echo $mins ?> minutes,
<? echo $secs ?> seconds
<? echo $text ?>
</p>
June 9th, 2003 at 1:45 am
excellent i was just going to ask about making it a year timer
June 22nd, 2003 at 9:41 am
hey, this is a great bit of code! i used a little bit of it in a script that i just wrote (a pregnancy info script), and i plan to distribute my script. i want to make sure i’m not stepping on your toes before i do, so i thought i’d post the link to my script so you can see how much of your code i used and the little part where i give scriptygoddess.com credit. let me know if you have any comments!
http://usr-bin-mom.com/pregnancy.txt
August 9th, 2003 at 2:13 pm
I can’t seem to get it to show up on the main page of my weblog. I inserted it like such where I wanted it placed on the page.
include(’/HOME/PUBLIC_HTML/cgi-bin/vote_countdown.php’);
August 9th, 2003 at 2:22 pm
It’s probably case sensitive. Also, is that how the paths work for your host? Mine requires something like /home/~username/public_html/path to file. The username would be replaced with your username, of course.
January 21st, 2004 at 10:50 pm
This is just what I was looking for to express the time I’ve been blogging in years, months, and days. I currently have a js to do that, but returns # days only. Tried this and it works…but it’s way off, accuracy wise. In my test, the correct days blogging is 266. Using this script returned 0 years, 8 months, 4 days. Even if every month had 31 days, that’s waaaayyy off. Any idea why? Would much prefer php vs. js!
January 22nd, 2004 at 8:42 am
Ugh!!! Ok - I will look into it. You’ll be hearing from me
January 22nd, 2004 at 8:05 pm
Figured it out - you can’t just remove part of the display - ie. if you don’t have it display the number of weeks - it will still have removed those from the “count” - the calculation was correct… with the full display it would have shown:
0 years, 8 months, 3 weeks, 4 days… that IS correct
January 22nd, 2004 at 8:49 pm
Taking a break from my tasty crow dinner, here’s a modification to the last full script code posted here if you only want years, months, and days to dispaly as the output. This orphans the hours, minutes, seconds…but I think that’s livable! There may be a more elegant way to do this…just know it worked when I tested it. Replace the last section with this code:
<p>
<? echo $years ?> years,
<? echo $months ?> months,
<? echo $weeks*7+$days ?> days
<? echo $text ?>
</p>
August 13th, 2004 at 11:01 am
I’d like to also specify the hours and minutes in the start date.
September 19th, 2004 at 11:33 am
The countdown script only works for a few years… I tried to use it for a lifespan script showing how much time I have left as an average life span… if I put in 2037 or before that as the year it works fine, if I put in 2038 or later it breaks and says they day is already over.
anyone have an answer to this?
September 25th, 2004 at 8:55 am
This is exactly what I was looking for! How can I input a specific *time* of day (hrs, min, secs)?
Thanks
September 25th, 2004 at 4:10 pm
It’s been awhile since I worked on this script - but since the start time is being created using the “strtotime” function, I would imagine you could simply do this too:
$start = “2004-09-25 09:20:00″
September 25th, 2004 at 4:11 pm
Should add that that line should end with a semicolon… so here it is again:
$start = “2004-09-25 09:20:00″;
September 25th, 2004 at 11:17 pm
Thanks for re-visiting this Jennifer! And for your help