strayduck Posted July 3, 2008 Share Posted July 3, 2008 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! Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 3, 2008 Share Posted July 3, 2008 http://us3.php.net/date date("format string", $timestamp); set $timestamp to whatever unix timestamp you want to be formated by date. Quote Link to comment Share on other sites More sharing options...
TransmogriBenno Posted July 3, 2008 Share Posted July 3, 2008 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; ... ?> Quote Link to comment Share on other sites More sharing options...
mbeals Posted July 3, 2008 Share Posted July 3, 2008 yea you're right. date() won't do it. It's still early Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 3, 2008 Share Posted July 3, 2008 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. Quote Link to comment Share on other sites More sharing options...
strayduck Posted July 3, 2008 Author Share Posted July 3, 2008 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... Quote Link to comment Share on other sites More sharing options...
discomatt Posted July 3, 2008 Share Posted July 3, 2008 Post your code and we'll let you know. Here's a hint though... the date function can tell you if a given date is a leap year and how many days are in a given month. Time stamps are your friend here Quote Link to comment Share on other sites More sharing options...
strayduck Posted July 6, 2008 Author Share Posted July 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.