1

How would you write: hour(s) (and) minute(s) in a sentence? Is there a clean solution?

Like: There are 12hrs and 1min left.

I have a $difference value to convert (seconds between two timestamps.)

Examples of desired output:

  • 3hrs and 12mins
  • 1hr and 1min
  • 12hrs
  • 43mins
  • 1hr
  • 1min

NOT:

  • 3hrs and 1mins
  • 1hrs and 0mins
  • 0hrs and 0mins
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
Chris
  • 893
  • 10
  • 23
  • possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](http://stackoverflow.com/a/18602474/67332) – Glavić Jan 09 '14 at 23:28
  • I don't quite agree with this being a duplicate with the referenced, as this request is not dealing with past time, but rather future time. These two questions are specifically different. – Chris Jan 10 '14 at 01:53

1 Answers1

1

I shall assume you know how to convert a number of seconds into $hours and $minutes. From there:

$out = "";
if( $hours) $out .= $hours."hr";
if( $hours > 1) $out .= "s";
if( $hours && $minutes) $out .= " and ";
if( $minutes) $out .= $minutes."min";
if( $minutes > 1) $out .= "s";
if( !$out) $out = "now";
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592