2

I have a form input that accepts time interval strings such as "1 day", "3 weeks", "2 months, 10 days" etc and converts them to integer representations of the total seconds, so "1 day" becomes 86400.

Now I need to be able to retrieve the integer and convert it back to a formatted string to be editable.

I've tried playing around with DateInterval but I've not had much luck. Besides, from what I gather that would restrict me to only displaying it in one format, so it could show "3 days" or "58 days".

Anyone know a script that can convert a duration timestamp to the most appropriate interval format string?

Bobe
  • 2,040
  • 8
  • 29
  • 49
  • possible duplicate of **[Convert seconds into days, hours, minutes and seconds](http://stackoverflow.com/a/19680778/67332)** – Glavić Mar 31 '14 at 09:12
  • Well that's embarrassing, the duplicate question reminded me that I already had a working script buried in my helper libraries. Thanks anyway guys. – Bobe Mar 31 '14 at 09:21

1 Answers1

-1

Something like this:

<?php
$secs=7*106400;
$days=(int)($secs/(60*60*24));
$weeks=(int)($days/7);
$days=$days%7;
$remaining=$secs%(60*60*24);
$hours=(int)($remaining/(60*60));
$remaining=$remaining%(60*60);
$minutes=(int)($remaining/(60));
$remaining=$remaining%(60);
echo $weeks.' weeks '.$days.' days '.$hours.' hours '.$minutes.' minutes '.$remaining.' seconds';

Output:

1 weeks 1 days 14 hours 53 minutes 20 seconds
CodeBird
  • 3,883
  • 2
  • 20
  • 35