Jump to content

Simple "Uptime" counter with a twist


strayduck

Recommended Posts

I want to work out the amount of time that has passed between two dates. One date is the date that a system went live, and the other date is the current date. Accuracy needs to be to the second.

 

The easy way of doing this is to use the time the system went live as the Unix timestamp for that particular date and time, and then use the php function time() so that:

 

$datetime_systemlive = some unix timestamp;
$uptime = time() - $datetime_systemlive;

 

BUT

 

I want to be able to give the system uptime as a preformatted string, so that instead of showing an integer number of seconds it shows:

 

"x years, y months, z days, a hours, b minutes, c seconds"

 

Any pointers of how to make that string? Can I use the date() function at all?

 

All help gratefully received!

Link to comment
Share on other sites

I don't think date is an appropriate solution to the problem, because the OP is looking for time elapsed, which isn't a timestamp value (it's the difference between two timestamp values).

 

If you want to use terms like 'years', 'months' etc. then you need a proper function that cares about how many days in each month, whether there are leap years, etc. If you just want days or weeks, then all you need to do is just divide the seconds into the relevant fields:

 

<?php
$seconds_per_minute = 60;
$seconds_per_hour = $seconds_per_minute * 60;
$seconds_per_day = $seconds_per_hour * 24;
$seconds_per_week = $seconds_per_day * 7;

$weeks = floor ($uptime / $seconds_per_week);
$uptime -= $weeks * $seconds_per_week;

$days = floor ($uptime / $seconds_per_day);
$uptime -= $days * $seconds_per_day;

...
?>

Link to comment
Share on other sites

Yeah, if you wanna go any where beyond weeks you have to remember there are a variable amount of days in a month, and days in a year, so those have to be looped and checked. It won't be a very efficient function.

 

I have a function that does this if you'd still like to see.

Link to comment
Share on other sites

I thought it may be more complicated than throwing a standard PHP function at it!

 

I'm going to have a go at writing the function myself...I'm still pretty new to this and could do with the exercise. Although any more examples I can use for comparison (and to show how hideously inefficient my code is!) would be much appreciated...

 

 

Link to comment
Share on other sites

This is effort no. 1.

 

<?
// Simple Uptime Calculator
// Generate a Unix timestamp for my date of birth.

// Store required date
$dseconds = 0;
$dminutes = 30;
$dhours = 2;
$dmonth = 11;
$dday = 22;
$dyear = 1984;

// Make timestamp from supplied date
$d = mktime($dseconds,$dminutes,$dhours,$dmonth,$dday,$dyear);
$now = time();

//So simple arithmetic and echo a result in seconds
$seconds = $now-$d;
echo "Time elapsed since birth: " . $seconds . "<br />";

//Work out complete minutes, left over seconds
$minutes = floor($seconds/60);
$seconds = $seconds%60;
// Work out complete hours, left over minutes, and echo
$hours = floor($minutes/60);
$minutes = $minutes%60;
// Work out complete days, left over hours, and echo
$days = floor($hours/24);
$hours = $hours%24;

//Determine leap years between current and past date
for ($i=date(Y);$i!=$dyear;$i--) {
if (date(L,mktime(1,1,1,1,1,$i)) == 1) {
	$leap = $leap + 1;
}
}
//Work out complete years, left over days (plus however many leap days we missed) and echo
$years = floor($days/365);
$days = ($days%365)+($leap);
echo "Reformatted time elapsed since birth: " . $years . " years, " . $days . " days, " . $hours . " hours, " . $minutes . " minutes and " . $seconds . " seconds<br />";
?>

 

All comments welcome...as a reminder I am very new to this so any tips are good tips!

 

I did run into trouble with the month - I don't think it is possible to implement it. As an example, if you were born on 14 March and you wanted to know how many months it has been from then up to today (6 July) you can't accurately state it as the months have varying numbers of days. All I can think of is using 'lunar' months of 28 days but there doesn't seem much point so I stuck with years, days, hours, minutes, seconds.

 

Admittedly this isn't a very cool function or an innovative piece of programming...but thank you all for your help with it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.