def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=' '):
"""
Takes an amount of seconds and turns it into a human-readable amount of time.
"""
# the formatted time string to be returned
time = []
# the pieces of time to iterate over (days, hours, minutes, etc)
# - the first piece in each tuple is the suffix (d, h, w)
# - the second piece is the length in seconds (a day is 60s * 60m * 24h)
parts = [(suffixes[0], 60 * 60 * 24 * 7 * 52),
(suffixes[1], 60 * 60 * 24 * 7),
(suffixes[2], 60 * 60 * 24),
(suffixes[3], 60 * 60),
(suffixes[4], 60),
(suffixes[5], 1)]
# for each time piece, grab the value and remaining seconds, and add it to
# the time string
for suffix, length in parts:
value = seconds / length
if value > 0:
seconds = seconds % length
time.append('%s%s' % (str(value),
(suffix, (suffix, suffix + 's')[value > 1])[add_s]))
if seconds < 1:
break
return separator.join(time)
if __name__ == '__main__':
# 2 years, 1 week, 6 days, 2 hours, 59 minutes, 23 seconds
# 2y 1w 6d 2h 59m 23s
seconds = (60 * 60 * 24 * 7 * 52 * 2) + (60 * 60 * 24 * 7 * 1) + (60 * 60 * 24 * 6) + (60 * 60 * 2) + (60 * 59) + (1 * 23)
print elapsed_time(seconds)
print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'])
print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True)
print elapsed_time(seconds, [' year',' week',' day',' hour',' minute',' second'], add_s=True, separator=', ')
Tag Archive for time
elapsed_time (human readable time span given total seconds)
Elapsed time string from time in seconds
function elapsedTime (createdAt)
{
var ageInSeconds = (new Date().getTime() - new Date(createdAt).getTime()) / 1000;
var s = function(n) { return n == 1 ? '' : 's' };
if (ageInSeconds < 0) {
return 'just now';
}
if (ageInSeconds < 60) {
var n = ageInSeconds;
return n + ' second' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60) {
var n = Math.floor(ageInSeconds/60);
return n + ' minute' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60 * 24) {
var n = Math.floor(ageInSeconds/60/60);
return n + ' hour' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60 * 24 * 7) {
var n = Math.floor(ageInSeconds/60/60/24);
return n + ' day' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60 * 24 * 31) {
var n = Math.floor(ageInSeconds/60/60/24/7);
return n + ' week' + s(n) + ' ago';
}
if (ageInSeconds < 60 * 60 * 24 * 365) {
var n = Math.floor(ageInSeconds/60/60/24/31);
return n + ' month' + s(n) + ' ago';
}
var n = Math.floor(ageInSeconds/60/60/24/365);
return n + ' year' + s(n) + ' ago';
}
// Make date parseable in IE
function fixDate (d)
{
var a = d.split(' ');
var year = a.pop();
return a.slice(0, 3).concat([year]).concat(a.slice(3)).join(' ');
}
Category: Uncategorized |
Tags: conversion, convert, elapsed, function, javascript, js, seconds, time
Simple Time Delay
//at frame 1
delay = 5000; //milliseconds
function playMovie() {
gotoAndPlay(_currentframe + 1);
clearInterval(pauseInt);
}
//at the end of the animation or between of your animations
stop();
pauseInt = setInterval(playMovie, delay);
simple 24 hour time (MySQL or military) to 12 hour time conversion
<?=date("g:i a", strtotime("13:30"));?>
<?=date("H:i", strtotime("1:30 pm"));?>
Time comparation in Java
public static boolean isDateBefore(String date1,String date2){
try{
DateFormat df = DateFormat.getDateTimeInstance();
return df.parse(date1).before(df.parse(date2));
}catch(ParseException e){
System.out.print("[SYS] " + e.getMessage());
return false;
}
}
WordPress Snippet to set Post Expiration time and date
***BEFORE LOOP
<?php
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
?>
***LOOP HERE***
***AFTER LOOP***
<?php
}
?>
Relative Time
function plural($num) {
if ($num != 1)
return "s";
}
function getRelativeTime($date) {
$diff = time() - strtotime($date);
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}
human readable millisecond timestamp
<script>
function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}
function time(ms) {
var sec = Math.floor(ms/1000)
ms = ms % 1000
t = three(ms)
var min = Math.floor(sec/60)
sec = sec % 60
t = two(sec) + ":" + t
var hr = Math.floor(min/60)
min = min % 60
t = two(min) + ":" + t
var day = Math.floor(hr/60)
hr = hr % 60
t = two(hr) + ":" + t
t = day + ":" + t
return t
}
document.write(time(12034056070))
</script>
SQL Date/Time Format
var myDate:String = new String("");
var today:Date = new Date();
myYear = String(today.getFullYear());
myDay = String(today.getUTCDate());
if(myDay.length == 1) myDay = "0" + myDay;
myMonth = String(today.getMonth() + 1);
if(myMonth.length == 1) myMonth = "0" + myMonth;
myHours = String(today.getHours());
if(myHours.length == 1) myHours = "0" + myHours;
myMinutes = String(today.getMinutes());
if(myMinutes.length == 1) myMinutes = "0" + myMinutes;
mySeconds = String(today.getSeconds());
if(mySeconds.length == 1) mySeconds = "0" + mySeconds;
myDate = myYear + "-" + myMonth + "-" + myDay + " " + myHours + ":" + myMinutes + ":" + mySeconds;
Date & Time Basics
// Set timezone
putenv('TZ=Australia/Adelaide');
// Date formatting
date("m.d.y"); // 03.10.01
date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
date("D M j G:i:s T Y"); // Sat Mar 10 17:16:08 MST 2001
date("l F j, Y"); // Saturday March 10, 2001
date("H:i:s"); // 17:16:17
date(DATE_RFC822); //RFC822 format date for RSS
// Change date from MySQL format
list($year,$month,$date) = explode('-',$mysql_date);
$new_date = date("D M j Y", mktime(0,0,0,$month,$date,$year));
// Get UNIX timestamp from date and time (hr-min-sec-month-day-year)
$stamp = mktime(0, 0, 0, 3, 0, 2000);