scriptygoddess

23 May, 2002

Count up timer in PHP

Posted by: Jennifer In: Scripts

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

Here's a demo of the above.

28 Responses to "Count up timer in PHP"

1 | Christine

May 24th, 2002 at 2:44 am

Avatar

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!)

2 | Liz

May 27th, 2002 at 10:38 am

Avatar

Is there a simple way of just changing a line to make it a count down timer, instead?

Thanks,
Liz

3 | Jennifer

May 27th, 2002 at 10:12 pm

Avatar

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…

4 | Lynda

May 28th, 2002 at 9:49 am

Avatar

Woo! Love it!!! This is most certainly very useful.

5 | Christine

June 2nd, 2002 at 4:31 am

Avatar

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!

6 | Jennifer

June 2nd, 2002 at 6:08 am

Avatar

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

7 | Jennifer

June 2nd, 2002 at 6:13 am

Avatar

Demo and download have been updated. 😀

8 | eve

July 13th, 2002 at 5:31 pm

Avatar

As always… thank you so much for this! works perfectly!

9 | dave

July 30th, 2002 at 10:02 pm

Avatar

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?

10 | Jennifer

July 30th, 2002 at 10:13 pm

Avatar

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

11 | rc

September 1st, 2002 at 7:33 am

Avatar

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?

12 | Christine

September 14th, 2002 at 12:23 pm

Avatar

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*

13 | The Geek

March 23rd, 2003 at 10:11 am

Avatar

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>

14 | The Geek

March 23rd, 2003 at 10:28 am

Avatar

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>

15 | james

June 9th, 2003 at 1:45 am

Avatar

excellent i was just going to ask about making it a year timer

16 | michelle

June 22nd, 2003 at 9:41 am

Avatar

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

17 | Sid

August 9th, 2003 at 2:13 pm

Avatar

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');

18 | Christine

August 9th, 2003 at 2:22 pm

Avatar

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.

19 | Gary

January 21st, 2004 at 10:50 pm

Avatar

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!

20 | Jennifer

January 22nd, 2004 at 8:42 am

Avatar

Ugh!!! Ok – I will look into it. You'll be hearing from me :)

21 | Jennifer

January 22nd, 2004 at 8:05 pm

Avatar

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 :)

22 | Gary

January 22nd, 2004 at 8:49 pm

Avatar

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>

23 | Lonney

August 13th, 2004 at 11:01 am

Avatar

I'd like to also specify the hours and minutes in the start date.

24 | MeagerBox

September 19th, 2004 at 11:33 am

Avatar

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?

25 | Frank

September 25th, 2004 at 8:55 am

Avatar

This is exactly what I was looking for! How can I input a specific *time* of day (hrs, min, secs)?

Thanks

26 | Jennifer

September 25th, 2004 at 4:10 pm

Avatar

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"

27 | Jennifer

September 25th, 2004 at 4:11 pm

Avatar

Should add that that line should end with a semicolon… so here it is again:
$start = "2004-09-25 09:20:00";

28 | Frank

September 25th, 2004 at 11:17 pm

Avatar

Thanks for re-visiting this Jennifer! And for your help :)

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements