Here's one using the DateTime Class. Kind of similar to radzserg's answer. This will only display the units necessary, and negative times will have the 'ago' suffix All in one solution. Gives no units with zeroes. Will only produce number of units you specify 3 by default. Quite long, perhaps not very elegant. Defines are optional, but might come in handy in a big project.
With DateInterval :. Here's some code that I like to use for the purpose of getting the duration between two dates. It accepts two dates and gives you a nice sentence structured reply. This is a slightly modified version of the code found here. I am editing one of the code to work it well when negative value comes.
So we need to use abs function before using it in the floor function. It also removes unnecessary precision e. This code is in Codeigniter. I used it in my case, you can use whatever you want. This is a function i used in the past for substracting a date from another one related with your question, my principe was to get how many days, hours minutes and seconds has left until a product has expired :.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Convert seconds into days, hours, minutes and seconds Ask Question. Asked 9 years, 11 months ago. Active 24 days ago. Viewed k times. Florian Florian 1, 3 3 gold badges 15 15 silver badges 21 21 bronze badges. Add a comment. Active Oldest Votes. Pikamander2 5, 3 3 gold badges 38 38 silver badges 56 56 bronze badges. Be sure to add validation to the function. Nevertheless, your validation is still wrong, because for example it will also pass alphabet.
What does the mean when passed as an argument to the DateTime constructor? IvankaTodorova: value after is unix timestamp. I think this example could be improved.
Show 6 more comments. Matthew Julian Moreno Julian Moreno 3 3 gold badges 15 15 silver badges 29 29 bronze badges. It would be nice to include source — Martin. This doesn't work as expected for days. This should be 1 day and 3 hours. Show 1 more comment. Luke Cousins Luke Cousins 1, 1 1 gold badge 18 18 silver badges 37 37 bronze badges.
Very nice Luke, keeping it compact and clean! Community Bot 1 1 1 silver badge. Simple yet efficient. Although I didn't like the 'Months' bit. You should include code in your answer, not link to another page. There is no way to ascertain that the website to which you are linking will still be there tomorrow — Zachary Weixelbaum.
Thank you so much for your effort to provide us simple solutions — Adnan. I built on Glavic's answer to add some extra features that I needed; Don't print zeros.
So "5 minutes" instead of " 0 hours, 5 minutes" Handle plural properly instead of defaulting to the plural form. Limit the output to a set number of units; So "2 months, 2 days" instead of "2 months, 2 days, 1 hour, 45 minutes" You can see a running version of the code here. Ramy Nasr Ramy Nasr 2, 18 18 silver badges 22 22 bronze badges. Further explanation would go a long ways in this answer, such as what the integer constants represent and how string formatting works with sprintf.
But probably the best solution here. When you have days, it begins to look Trent Renshaw Trent Renshaw 7 7 silver badges 13 13 bronze badges. Artur Artur 6, 2 2 gold badges 24 24 silver badges 35 35 bronze badges.
James Doherty James Doherty 6 6 silver badges 5 5 bronze badges. So the problem is realistic: Why does not an hour contain minutes? We have to survive the puzzle and wait for a solution. If a man is called to be a streetsweeper, he should sweep streets even as Michelangelo painted or Beethoven composed music or Shakespeare wrote poetry. He should sweep streets so well that all the hosts of heaven and earth will pause and say, 'Here lived a great streetsweeper who did his job well.
Life calculator. Working days. IT converters. Hours to minutes converter. Guest: How many minutes in an hour? One hour is equal to 60 minutes. Respectively one minute is 0. Two hours are equal to minutes. Obviously, two minutes are 0. Date conversion table Millennium. Guest: Stimulating puzzles can appear in people's minds: How long is an hour? John: We can follow the step to solve the problem. First of all, we require to do is to multiply the value of hours by Jim: When is an hour equivalent to a minute?
Jim: How many minutes are in one minute? If possible please share a excel formula for this. Desi: Would someone please show the equation Multiply 6 hours and 23 minutes by I'm on my way to happiness. Where I can find some peace and rest. Peter Tosh. I do myself a greater injury in lying that I do him of whom I tell a lie.
0コメント