time snippets

How to format number of seconds as duration with PHP

Tagged duration, format, time, seconds, php  Languages php

A very sophisticated algorithm that will display the length of, for example, a video as 12:01:30.

function duration($seconds_count)
        $delimiter  = ':';
        $seconds = $seconds_count % 60;
        $minutes = floor($seconds_count/60);
        $hours   = floor($seconds_count/3600);

        $seconds = str_pad($seconds, 2, "0", STR_PAD_LEFT);
        $minutes = str_pad($minutes, 2, "0", STR_PAD_LEFT).$delimiter;

        if($hours > 0)
            $hours = str_pad($hours, 2, "0", STR_PAD_LEFT).$delimiter;
            $hours = '';

        return "$hours$minutes$seconds";

A custom Swedish time_ago_in_words implementation

Tagged time_ago_in_words, ruby, rails, time, swedish, svenska, inflections  Languages ruby

This is an easily customizable time_ago_in_words implementation in Swedish that will produce this output:

< 5 minuter
< 45 minuter
< 1 timme
> 2 timmar
> 1 dag, 2 timmar
20 April 2010 (if more than 31 days ago)

The code:

class Time
  def time_ago_in_words
    words = ''
    timestamp = self
    if timestamp
      time_until = (Time.now.to_datetime - timestamp.to_datetime)
      days = time_until.to_i
      hours, minutes, seconds, frac = Date.day_fraction_to_time(time_until)
      hours = hours % 24

      if days == 0
        if hours < 1
          words = case minutes
            when 0..4 then "< 5 minuter"
            when 5..14 then "< 15 minuter"
            when 15..29 then "< 30 minuter"
            when 30..44 then "< 45 minuter"
            when 45..59 then "< 1 timme"
          words = "> #{hours} #{'timme'.inflect(hours)}"
      elsif days < 31
        words = "> #{days} #{'dag'.inflect(days)}"
        if hours > 0
          words += ", #{hours} #{'timme'.inflect(hours)}"
        words = timestamp.l(:format => :daymonthyear)

You'll need this for inflections:

class String
  def inflect(count)
    count > 1 ? ActiveSupport::Inflector.pluralize(self) : self

Add this to config/locales/sv-SE.yml for localizing the date in Swedish:

      daymonthyear: "%d %B %Y"

Add this to config/initializers/inflections.rb for Swedish inflections:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'dag', 'dagar'
  inflect.irregular 'vecka', 'veckor'
  inflect.irregular 'månad', 'månader'
  inflect.irregular 'timme', 'timmar'
  inflect.irregular 'minut', 'minuter'
  inflect.irregular 'sekund', 'sekunder'

How to integrate the Any+Time jQuery date-select plugin with Rails

Tagged ruby, date, time, select, anytime, rails  Languages ruby

Add the following to your AR model:

def send_at_formatted
   self.send_at.strftime('%d.%m.%Y') if self.send_at?

def send_at_formatted=(value)
   self.send_at = Time.zone.parse(value)

Add the following to your JavaScript file:

$(document).ready(function() {
  $('.date-select').each(function(index, element) {
    var elem = $(element),
        options = { format: '%d.%m.%Y', labelTitle: elem.data('title') };

    AnyTime.picker( elem.attr('id'), options );

Add this to your HAML/ERB view:

= f.text_field :send_at_formatted, :class => 'date-select', :"data-title" => 'Send at'

Download Any+Time JS and CSS files and include them in your head tag.

Ruby and Ruby on Rails Time and Time Zones

Tagged ruby, time, date, timezone, use_zone, dst, current, local, now  Languages 


  • Parsing: What is the time zone of the input?
  • Parsing: Use Time.zone.parse to parse user input, but first set Time.zone to the user’s time zone.
  • UI: What is the user’s time zone?
  • UI: Show the time in the user’s time zone: https://github.com/github/time-elements
  • Ruby: Time.now uses the operating system’s time zone
  • Ruby: Date.today uses the operating system’s time zone
  • Ruby: Time.parse uses the operating system’s time zone
  • Ruby: Use Range.cover? with time ranges and know how it differs from Range.include?
  • Rails: Time.zone.now and Time.current uses Rails’ time zone (config.time_zone in config/application.rb)
  • Rails: Time.zone.today uses Rails’ time zone
  • Rails: Time.zone.parse uses Rails’ time zone
  • Database: Store time in UTC (without time zone information)
  • Database: “BETWEEN a AND b” is inclusive
  • Database: PostgreSQL’s current_timestamp uses the database time zone (timezone in postgresql.conf)
  • API: Use ISO 8601 format
  • Daylight savings
  • Leap year
  • Falsehoods

Total hours between start and end time - Google Spreadsheet

Tagged time, time report, format, google, spreadsheet  Languages 

I want to extract the total hours worked from a Google spreadsheet.

Given the following data in a Google spreadsheet:

  • End time in column D using time format
  • Start time in column C using time format

For example:


To achieve this in a Google spreadsheet we can use the following formula:


Reference: https://infoinspired.com/google-docs/spreadsheet/payroll-hours-time-calculation-in-google-sheets/